我用asp.net写了一个程序,如何才能控制同一个用户名不能同时在不同的机器上登录啊,要提示他此用户已在其他地方登录拉

解决方案 »

  1.   

    你在Session_End事件那里更新用户的状态不可以吗?
      

  2.   

    用Application来记录所有在线的用户列表,然后判断.
      

  3.   

    加上用Session,过期后自动变成未登录状态
      

  4.   

    njdxbzu(夏三典)
    怎么记录啊,怎么判断啊
    能给点代码吗
    谢谢大侠
      

  5.   

    http://blog.csdn.net/lengyubing_1983/archive/2006/05/23/751380.aspx
      

  6.   

    try
    {
    string sKey = this.txtUserName.Text.Trim()+"_"+this.txtPassword.Text;
    string sUser = string.Empty;
    sUser=Convert.ToString(Cache[sKey]);
    if (sUser == string.Empty || sUser==null || sUser=="")
    {
    TimeSpan SessTimeOut = new TimeSpan(0,0,0,this.Session.Timeout,0);
    HttpContext.Current.Cache.Insert(sKey,sKey,null,DateTime.MaxValue,SessTimeOut,
    System.Web.Caching.CacheItemPriority.NotRemovable,null);
    Session["User"] = sKey;
    string validateCode=this.txtValidateCode.Text.ToUpper();if(Session["ValidateCode"]!=null)
    {
    if(validateCode=="" || validateCode!=Session["ValidateCode"].ToString().ToUpper())
    this.Response.Redirect(this.Request.ApplicationPath+"sitemessage/message.aspx?msgid=12",true);
    }
    else
    {
    this.Server.Transfer("/sitemessage/message.aspx?msgid=12",true);
    }
    bool result = (new RegUserService()).UserLogin(this.txtUserName.Text,this.txtPassword.Text,Convert.ToInt32(this.ddlCook.SelectedValue));if(!result)
    {
    this.Response.Redirect(this.Request.ApplicationPath+"sitemessage/message.aspx?msgid=11",true);
    }
    else
    {
    RegUserService.UpdateUserOnlineState(Visitor.Current.UserID);
    this.Response.Redirect(this.Request.ApplicationPath+"default.aspx",true);//转向主页
    }
    }
    else
    {
    error="<script language='javascript'>alert('用户已登录,请不要重复登录!');";
    error=error+"location.replace('/Default.aspx')</script>";
    return;
    }
      

  7.   

    string sKey = this.txtUserName.Text.Trim()+"_"+this.txtPassword.Text;
    string sUser = string.Empty;
    sUser=Convert.ToString(Cache[sKey]);
    if (sUser == string.Empty || sUser==null || sUser=="")
    {
    TimeSpan SessTimeOut = new TimeSpan(0,0,0,this.Session.Timeout,0);
    HttpContext.Current.Cache.Insert(sKey,sKey,null,DateTime.MaxValue,SessTimeOut,
    System.Web.Caching.CacheItemPriority.NotRemovable,null);
    Session["User"] = sKey;
    ..
    ..//做你想做的事情
    ..else
    {
     //用户已登录.
    }
    如果用户关闭浏览器,只能等到SESSION过期了才能登录.
      

  8.   

    解决用户关闭浏览器问题,可以用JS来捕捉IE的关闭事件,在该事件中跳转到一个注销页面,这个页面实现注销的功能。
      

  9.   

    楼主有理
    在body unload事件中 转向到一注销网页即可 只是还不能解决session超时问题
      

  10.   

    Application["OnlineUser"]中保存登陆用户列表
    ,在登陆的时候判断Application["OnlineUser"]是否为空
    ,不为空就就判断是否存在指定用户
    if (Application["OnlineUser"] != null)
    {
        ArrayList OnlineUserArr = (ArrayList)Application["OnlineUser"];
        if (OnlineUserArr.IndexOf(userName) >= 0) //
        {
            //TODO:执行用户已登陆的处理
        }
        else
        {
           Application.Lock();
           OnlineUserArr.Add(userName);
           Application["OnlineUser"] = OnlineUserArr;
           Session["UserName"]=userName;//用于Session_End中删除和其他用处
           Application.UnLock();
        }
    }
    另外在Session_End中执行从Application中删除的命令
    Application.Remove(userName);
    这个userName可以从Session["UserName"]取.但是在一个用户退出并在Session失效时间前重新登陆的话这里可能要增加对IP的判断看是否需要拒绝登陆,就是Application和Session中跟用户一起记录IP信息.Session_End只对Session保存在内存中有效,保存到数据库或状态服务器无效.
      

  11.   

    另外说一句,不用纯粹用Session去保存登陆的用户列表,同一个用户在不同地方登陆其SESSION是不互访的.也不要使用cache,并不保证cache是否只有创建的用户可以访问,而且它可能会自动清理的.无只做了一个Asp.net小程序,有些认识可能不对,希望大家指正.