restrict second login while first login session is active

1.3k Views Asked by At

In case of Asp.NET web application, using state server session and web farm; How to restrict user from login multiple times. If user is already logged in, He shouldn't be allowed to login using the same credential from different browser. I have found a link to automatically log out previous session Single Session Enforcement. But the requirement is to keep the first session alive rather than logging him out. whenever second session tries to login, he should get error message stating "User is already logged in".

We have session time out of 20 min.

Thank you in advance.

2

There are 2 best solutions below

5
Ajay On

For this you have to set IsLoggedin status in you DB. While login you have to check this status. If it's true then user is already logged in. And while logout you have to change this to false.

If user closed the browser

 $(function () {
        //logic to signout user when he closes browser
        window.onclick = function () {
            clicked = true;
        };

        window.onunload = function () {
            if (clicked == false) {
                $.get("/account/logout");
            }
        };
    });

In logout you can check if (GetUserContext() != null) then change IsLoggedIn status.

1
Pranav Bilurkar On

You can try this:for currect browser

if (!Session.IsNewSession && Request.UrlReferrer == null)
{
    // new 
}