如题:防止同时登陆。(asp.net)
     关于防止同时登录的问题,看了很多帖子,大多都是只提到了application,session 或者是通过数据库中的字段的状态来判断。但是都没有具体有用的实例或代码。
     本人再次向各位大侠赐教,小弟不胜感激

解决方案 »

  1.   

    放在登陆成功的地方:string key = TextBox1.Text; //用户名文本框设为cache关键字 
    string uer = Convert.ToString(Cache[key]); //读取cache中用户相应的值
    //判断cache中是否有用户的信息,如果没有相关的值,说明用户未登陆
    if (uer == null || uer == String.Empty)
    {
     //定义cache过期时间 TimeSpan SessTimeout = new TimeSpan(0, 0, System.Web.HttpContext.Current.Session.Timeout, 0, 0); //第一次登陆的时候插入一个用户相关的cache值,
     HttpContext.Current.Cache.Insert(key, key, null, DateTime.MaxValue, SessTimeout, System.Web.Caching.CacheItemPriority.NotRemovable, null);
     Session["ADMINID"] = TextBox1.Text;
     Response.Redirect("main.aspx");
    }
    else
    {
     //重复登陆
     Response.Write("<script>alert('您的账号已经登陆!');window.location='login.aspx';</script>");
    }
      

  2.   

    登陆时先判断你存放用户名的session 是否为空,为空给session 赋值登陆不空提示
      

  3.   

    1.把所有登陆用户存在这个全局Application中(建议把这个Application放在基类,方便调用):protected IList<IList> LoginInfo{get {if (Application["loginInfo"] == null){Application["loginInfo"] = new List<IList>();}return (IList<IList>)Application["loginInfo"];}}用户成功登陆时,保存登陆的用户名与登陆时间:DateTime dt = DateTime.Now;//这里保存当前用户的登陆状态,保存登陆信息,此处伪代码,存储可以用Session存储用户实体类,下面会用到.
    Session[“UserInfo”] = user.GetUserInfoByUName(userName, ref error);Session[“LastLoginDate”].LastLoginDate = dt;
    //这里把该用户名与登陆时间存到登陆用户列表中:IList al = new ArrayList();
    //这里我加的是用户名,因为这里是用用户名做主键的,其实用int更好.如果你是用int做用户主键,请用userid.
    al.Add(txtUserName.Text);al.Add(dt);LoginInfo.Add(al);页面里增加脚本,每隔一段时间(5秒)取一下状态,看看这个用户名是不是在其他地方登陆了,这里用到了prototype的ajax:<script language='javascript' type='text/javascript'>"function ValidSimpleUser(){new Ajax.Request('LeadNT.aspx?op=otherLogin', {onSuccess: function(transport) {if(transport.responseText == 1')top.location.href=''LeadNT.aspx?op=logout';}}); }setInterval('ValidSimpleUser();',5000);</script>脚本中用到了一个页面LeadNT.aspx,第一次调用返回一个值,如果是1 ,则表示别人登陆这个帐号了,第二次调用,是用这个页面来注销当前用户:
    LeadNT.aspx中的部分代码:
    //第一次调用:if (Request.QueryString["op"] == "otherLogin"){if (!string.IsNullOrEmpty(CUser.UserName)){foreach (IList li in LoginInfo){
    //此处是伪代码,如果自己写没这么复杂.一个实体类即可.
    if (li[0].ToString() == ((UserInfo)Session["UserInfo"]).UserName && DateTime.Parse(li[1].ToString()) > DateTime.Parse(Session["LastLoginDate"].ToString()){LoginInfo.Remove(li);Response.Write("1");break;return;}}}}
    //第二次调用if (Request.QueryString["op"] == "logout"){Response.Write("<script language='javascript' type='text/javascript'>alert('该帐号已在其他地方登录!');top.location.href=' "Logon.aspx?logout=true';</script>");}
      

  4.   

    http://dev.csdn.net/develop/article/65/65215.shtm
    http://www.cnblogs.com/caojinqin/archive/2008/11/04/1326275.html
    http://www.chenjiliang.com/Article/View.aspx?ArticleID=13417&TypeID=5
      

  5.   

    http://www.luojia.net/zz/2008/0721/article_9431.html
      

  6.   

    用 cache
    用户多时是不是对服务器要求较高?
      

  7.   


    //登录
     protected void ImageButton1_Click1(object sender, ImageClickEventArgs e)
        {
            DBConnClass loginDB = new DBConnClass();
            string passw = DBConnClass.Get_MD5_Method(this.TextBox1.Text.ToString().Trim());
            string login = loginDB.UserLogin(txtname.Text.Trim(), passw);
            if (login != null)
            {
                string key = txtname.Text;
                string uer = Convert.ToString(Cache[key]);
                if (uer == null || uer == String.Empty)
                {
                    TimeSpan SessTimeout = new TimeSpan(0, 0, System.Web.HttpContext.Current.Session.Timeout, 0, 0);
                    HttpContext.Current.Cache.Insert(key, key, null, DateTime.MaxValue, SessTimeout, System.Web.Caching.CacheItemPriority.NotRemovable, null);
                    Session["UsersName"] = txtname.Text.Trim();
                    if (ViewState["OldUrl"] != null)
                    {
                        string url = ViewState["OldUrl"].ToString();
                        ViewState["OldUrl"] = null;
                        Response.Redirect(url);
                    }
                    else
                    {
                        Response.Redirect("index.aspx");
                    }
                }
                else
                {
                    Response.Write("<script>alert('您的账号已经登陆!');window.location='login.aspx';</script>");
                }
            }
            else
            {
                msg.Text = "<script>alert(\"登录失败!用户名或密码错误!\")</script>";
            }
        }这样写了但是每次都是直接提示  “您的账号已经登陆!”  难道那代码不是放在登录按钮的事件中吗?
      

  8.   

    Page_Load
    {
    if(Session["username"]!=null)
    {
    Response.Write("<script>alert('你已经登录');</script>");
    return;
    }
    }
    btnLogin_Click
    {
    if(用户名密码正确)
    {
    Session["username"] = "用户名";
    }
    }
      

  9.   

    13 楼用session的好像不能实现单点登录吧,用cache的才可以实现。
      

  10.   

    string strUser=string.Empty; 
                    string strCacheKey = this.TextBox1.Text; 
     
                    strUser = Convert.ToString(Cache[strCacheKey]); 
     
                    if(strUser == string.Empty) 
                    { 
                        TimeSpan SessTimeOut = new TimeSpan(0,0,System.Web.HttpContext.Current.Session.Timeout,0,0); 
     
                        Cache.Insert(strCacheKey,strCacheKey,null,DateTime.MaxValue,SessTimeOut,CacheItemPriority.NotRemovable,null); 
                        Session["User"] = strCacheKey; 
                        this.Label1.Text = Session["User"].ToString(); 
                    } 
                    else 
                    { 
                        Response.Write("<script>alert('这个用户已经登录!')</script>"); 
                    } 
      

  11.   

    同意Application存储,这样在其他地方登录也可以判断.
      

  12.   

    在user表中,字段"yes"默认=0,登录时判断yes是否等于0,等于0,则执行登录验证,登录成功后"yes"=1,然后就再也不能登录了。
      

  13.   

    http://blog.csdn.net/lem12/archive/2007/06/06/1640512.aspx
      

  14.   

    但是用户如果是非法退出的话呢?那yes还是等于1啊    怎么让用户在非法退出的时候把它改为0啊?
      

  15.   


    貌似global.asax里有会话结束时间...
      

  16.   

    还是感觉存SEssion比较好!嘿嘿!
      

  17.   

    这样方法是可以  但是我发现当退出的时候必须要清除缓存Cache   还是那问题  当非法退出的是怎么去清除cache
      

  18.   

    判断用户  就要用 session 或者 Application 来存储和判断的 把登陆用户 存入session中
    先判断是否为空 
    然后  再判断  登陆的用户  时候和存储的用户  一致  如果一致 那么就拒绝登陆
      

  19.   


    这里只是判断cache中有没有当前用户信息,不需要做转换Convert.ToStringstring key = txtname.Text; if (Cache[key] == null){


      

  20.   

    登陆成功后,保存入session
    如果session已经存在该用户名,提示,不让登陆
      

  21.   

    使用数据库临时表,根据userid创建对应的临时表
    create proc useronlylogon
    @userId nvarchar(100)='testuser'
    as
    begin
    if  not(object_id('tempdb.dbo.#tmp' + @userId) is null)
    begin
    select 0
    end
    else
    begin
    declare @sql nvarchar(200)
    select @sql='create table #tmp' + @userId + '(UserName nvarchar(100))'
    exec(@sql)
    select 1
    end
    end
    登陆前用该存储过程去判断,返回为0表示该用户应经登陆,为1表示为登陆。log off的时候把这个临时表删除掉,临时表在服务器 down机的时候自动删除,这样比那种设置数据库字段更安全。
      

  22.   

    不好意思,一着急少写了个#,正确代码如下
    alter proc useronlylogon
    @userId nvarchar(100)='testuser'
    as
    begin
    if  not(object_id('tempdb.dbo.##tmp' + @userId) is null)
    begin
    select 0
    end
    else
    begin
    declare @sql nvarchar(200)
    select @sql = 'create table ##tmp' + @userId + '(UserName nvarchar(100)) ' + char(13)
    exec(@sql)
    select 1
    end
    end
      

  23.   

    是的,,以前做过也是这样,,,这种方法,,,就别想到解决吧,,,就像163.com的邮件功能也做不到
      

  24.   

    http://www.cnblogs.com/lovecherry/archive/2007/04/24/724878.html
      

  25.   

    to 52
    你可以用我在46楼里面写的存储过程,这样就不存在 session的问题了,即使即使数据库服务器down机也不会影响用户下次登陆
      

  26.   

    我采用的做法是, 登录时为用户生成一个唯一标识字符串(可用GUID), 存储在Cookie中, 然后根据此Cookie判断用户登录状态.如用户在其它地方登录时, 该字符串在服务端已经变化了, 那么先前登录的用户自动下线了
      

  27.   

    一个超简单也超笨的方法:
    设置一个字段,当用户登录时,置这个字段为登录状态,当用同一个用户名再次登录时,检测字段状态是否是登录
    if(是)
    {
        拒绝登录;
    }
    else
    {
        登录;
    }
      

  28.   

    以前有人问过这问题.已经解决了
    http://topic.csdn.net/u/20081023/09/9df24217-94a1-4c9b-81c6-911f5ff6de1a.html如果想要代码的话把邮箱留下
      

  29.   


    已发送:[email protected] --Hubery
      

  30.   

    我的处理方法是 在用户表增加一个保存临时密码的RandPwd字段
    当用户登录成功的时候 就随机生成一个加密的临时密码并保存在RandPwd中
    同时保存在本地的Cookie里面
    在验证用户是否登录的时候 比对临时密码 看是否一样
    不一样就说明已经在其他地方登录了
    这样子的方法有个限制  就是用户在同一时刻只能在一个地方登录
    在同一台机器上 你用IE登录后 再用FF登录也是不能同时登录的
      

  31.   

    请 潇湘夜雨给我一份代码20010566@163.com
      

  32.   

    [email protected] 也让我学习一下吧 谢谢
      

  33.   

     大哥 给我发一份学习学习下 谢谢
     [email protected]