Allow only a single login session per user

蹲街弑〆低调 提交于 2019-12-11 13:01:51

问题


Essentially I am asking this question here, but I am using ASP Identity instead of the ASP.Net Membership provider, and with that, that answer is of no use to me.


回答1:


Figured out how to do it. On login I call this:

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);

And this in the Global.asax

if (Session["username"] != null)
    {
        var cacheKey = Session["username"].ToString();
        if ((string) HttpContext.Current.Cache[cacheKey] != Session.SessionID) LogOut();
    }


来源:https://stackoverflow.com/questions/25359851/allow-only-a-single-login-session-per-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!