Allow only one concurrent login per user in ASP.NET

后端 未结 6 1199
渐次进展
渐次进展 2020-12-15 08:39

Is it possible to allow only one concurrent login per user in ASP.NET web application?

I am working on a web application in which I want to make sure that the websi

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-15 08:54

    You can create a cache entry per user and store their session ID in it. Session ID will be unique per browser session. In your login page, you can create that cache entry when they successfully login:

    if(Cache.ContainsKey["Login_" + username])
        // Handle "Another session exists" case here
    else
        Cache.Add("Login_" + username, this.Session.SessionID);
    

    (Code typed in textbox without syntax check. Assume "pseudo-code".)

    In global.asax you can then hook into the Session_End and expire that cache entry of the user. See this for the global.asax events.

    if(Cache.ContainsKey["Login_" + username])
        Cache.Remove("Login_" + username);
    

提交回复
热议问题