.net 环境下 防止多用户登陆

14 篇文章 0 订阅
protected void Login1_Authenticate(object sender,AuthenticateEventArgs e)
 
    {
              //验证 处理
              if (Membership.ValidateUser(Login1.UserName,Login1.Password))
              {
                      UserOnLine online = new UserOnLine();
                      //生成Key   
                      string sKey = Login1.UserName + "_IsLogin";
                      //得到Cache中的给定Key的值
                      string sUser = Convert.ToString(Cache[sKey]);
                      //检查是否存在   
                      if (sUser == null || sUser == String.Empty)
                      {
                              //Cache中没有该Key的项目,表明用户没有登录,或者已经登录超时         
                              //TimeSpan 表示一个时间间隔,获取系统对session超时作的设置值
                              //TimeSpan SessTimeOut = new TimeSpan(0, 0,System.Web.HttpContext.Current.Session.Timeout, 0, 0);
                              //(如果考虑到允许用户再次登陆的时间小于session超时时间,可将此值设小,在此示例中设置为一分钟)
                              TimeSpan SessTimeOut = new TimeSpan(0, 0, 1, 0, 0);
                              HttpContext.Current.Cache.Insert(sKey, sKey, null,DateTime.MaxValue, SessTimeOut,
                                System.Web.Caching.CacheItemPriority.NotRemovable, null);
                              //首次登录,您可以做您想做的工作了。
                              DataView dv = online.GetData(Login1.UserName);
                              if (dv.Count > 0)
                              {
                                      Login1.FailureText = "用户已登陆";
                                      e.Authenticated = false;
                              }
                              else
                              {
                                      online.UserName = Login1.UserName;
                                      online.Add();
                                      Session["UserKey"] = Login1.UserName;
                                      e.Authenticated = true;
                              }
                      }
                      else
                      {
                              Login1.FailureText = "用户已登陆";
                              e.Authenticated = false;
                      }
              }
      }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值