繁体   English   中英

如何每个用户只允许一个活动会话

[英]how to allow only one active session per user

我想实现一个代码,每个用户只能启用一个会话。 我使用asp.net进行表单身份验证。

我读了这篇文章: 在ASP.NET中,每个用户只能限制一个会话

但是我想做相反的事情,以断开以前的所有会话。

这是我的代码:

 void Application_Start(object sender, EventArgs e) { Application["UsersLoggedIn"] = new System.Collections.Generic.Dictionary<string, string>(); } 

当用户登录时,我插入一条记录:用户名,sessionID。 如果用户名存在,我只需更新sessionID

    void Application_BeginRequest(object sender, EventArgs e)
    {
        System.Collections.Generic.Dictionary<string, string> d = HttpContext.Current.Application["UsersLoggedIn"] as System.Collections.Generic.Dictionary<string, string>;
        if (d != null)
        {
            if (d.Count > 0)
            {
                    string userName = HttpContext.Current.User.Identity.Name;
                    if (!string.IsNullOrEmpty(userName))
                    {
                        if (d.ContainsKey(userName))
                        {
                            if (d[userName] != HttpContext.Current.Session.SessionID)
                            {
                                FormsAuthentication.SignOut();
                                Session.Abandon();
                            }
                        }
                    }
}
}

但是我得到了空的HttpContext.Current.User。 我也尝试了“ AuthenticatedRequest”,但是随后我得到了空会话。

有什么想法吗?

如果要访问HttpContext.Current.User和Session,则应使用AcquireRequestState事件。

如果您处于负载平衡的状态,并且有多台服务器,则需要以某种方式保持这种状态,那么当您需要释放用户锁(终止用户会话或注销)时,就会出现问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM