MVC 用户只能登陆一次 另一个地方登陆强制下线

 #region seesion,保证同一次会话的SessionID不变
        protected void Session_Start(object sender, EventArgs e) 
        { }


        protected void Session_End(object sender, EventArgs e)
        {
            Hashtable hOnline = (Hashtable)Application["Online"];
            if (hOnline != null)
            {
                if (hOnline[Session.SessionID] != null)
                {
                    hOnline.Remove(Session.SessionID);
                    Application.Lock();
                    Application["Online"] = hOnline;
                    Application.UnLock();
                }
            }

        }

        #endregion




        //每次用户登陆时均判断以下要登陆的用户名在Online中是不是已经存在,如果存在该用户名已经被登陆,将第一个人登陆的SessionID对应的用户名强制变更为_offline_,表示该登陆将被强制注销。
        //需要Global.cs里添加Session_Start和Session_End方法,保证同一次会话的SessionID不变
        private void HandleApplication(int userID)
        {
            HttpContext httpContext = System.Web.HttpContext.Current;
            Hashtable userOnline = (Hashtable)httpContext.Application["Online"];
            if (userOnline != null)
            {
                IDictionaryEnumerator idE = userOnline.GetEnumerator();
                string strKey = string.Empty;
                while (idE.MoveNext())
                {
                    if (idE.Value != null && idE.Value.ToString().Equals(userID.ToString()))
                    {
                        strKey = idE.Key.ToString();
                        userOnline[strKey] = "_offline_";
                        break;
                    }
                }
            }
            else
            {
                userOnline = new Hashtable();
            }
            userOnline[Session.SessionID] = userID.ToString();
            httpContext.Application.Lock();
            httpContext.Application["Online"] = userOnline;
            httpContext.Application.UnLock();
        }


   //定时检测是否被强制下线

    $(document).ready(function () {

//是否是正式环境
        if (isProduction == "true") {
            setInterval(function () {
                CheckIsForcedLogout();
            }, 10000);
        }
    });


    //检测是否被强制下线
    function CheckIsForcedLogout() {
        $.ajax({
            url: "/Home/CheckIsForcedLogout",
            type: "POST",
            dataType: "json",
            success: function (msg) {
                if (msg.OperateResult == "Success") {
                    $.messager.alert('', msg.OperateData, 'error', function () {
                        window.location.href = "/Account/Login";
                    });
                }
            },
            error: function (ex) { }
        });
    }


 /// <summary>
        /// 检测是否被强制下线
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult CheckIsForcedLogout()
        {
            try
            {
                HttpContext httpContext = System.Web.HttpContext.Current;
                Hashtable userOnline = (Hashtable)(httpContext.Application["Online"]);
                if (userOnline != null)
                {
                    IDictionaryEnumerator idE = userOnline.GetEnumerator();
                    string strKey = string.Empty;
                    if (userOnline.Count > 0)
                    {
                        while (idE.MoveNext())
                        {
                            //判断是否登录时保存的session是否与当前页面的sesion相同
                            if (userOnline.Contains(httpContext.Session.SessionID))
                            {
                                if (idE.Key != null && idE.Key.ToString().Equals(httpContext.Session.SessionID))
                                {
                                    //判断当前session保存的值是否为被注销值
                                    if (idE.Value != null && "_offline_".Equals(idE.Value.ToString()))
                                    {
                                        //验证被注销则清空session
                                        userOnline.Remove(httpContext.Session.SessionID);
                                        httpContext.Application.Lock();
                                        httpContext.Application["online"] = userOnline;
                                        httpContext.Application.UnLock();


                                        string msg = string.Format("下线通知:当前账号于 {0} 在另一地点 {1} 登录, 您被迫下线。若非本人操作,您的登录密码很可能已经泄露,请及时改密。",
                                            currentUser.LastLoginTime.ToString("yyyy-MM-dd HH:mm:ss"), currentUser.LastLoginIp);


                                        //登出,清除cookie
                                        FormsAuthentication.SignOut();


                                        return Json(new { OperateResult = ConstantDim.operateSuccess, OperateData = msg }, JsonRequestBehavior.AllowGet);
                                    }
                                }
                            }
                        }
                    }
                }
                return Json(new { OperateResult = ConstantDim.operateFailed }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(new { OperateResult = ConstantDim.operateFailed }, JsonRequestBehavior.AllowGet);
            }
        }







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值