Asp.net 每个用户只允许一次登录会话

Asp.net 每个用户只允许一次登录会话,asp.net,webforms,asp.net-identity-2,Asp.net,Webforms,Asp.net Identity 2,本质上我是在问,但我使用的是ASP身份,而不是ASP.Net成员资格提供商,因此,这个答案对我来说毫无用处。找到了解决方法。登录时,我称之为: var key = user.UserName; var timeOut = new TimeSpan(0, 0, HttpContext.Current.Session.Timeout, 0, 0); HttpContext.Current.Cache.Insert(key, Session.SessionID, null, DateTime.MaxV

本质上我是在问,但我使用的是ASP身份,而不是ASP.Net成员资格提供商,因此,这个答案对我来说毫无用处。

找到了解决方法。登录时,我称之为:

var key = user.UserName;
var timeOut = new TimeSpan(0, 0, HttpContext.Current.Session.Timeout, 0, 0);
HttpContext.Current.Cache.Insert(key, Session.SessionID, null, DateTime.MaxValue, timeOut, CacheItemPriority.NotRemovable, null);
这在Global.asax中


你所引用的答案可能重复,这正是你需要做的。对会员资格的引用非常少&你应该可以很容易地将它们更改为Identity。事实证明你是对的,但经过几个小时的挖掘,我发现。在没有任何数据库操作的情况下,这会不会有帮助?在Global.asax中的什么位置?在哪种方法中?现在记不起来了,但我可以在几个小时内检查一下
if (Session["username"] != null)
    {
        var cacheKey = Session["username"].ToString();
        if ((string) HttpContext.Current.Cache[cacheKey] != Session.SessionID) LogOut();
    }