User987
User987

Reputation: 3825

Limiting number of logged in users on .NET MVC application

I have done a fair amount of research on how to limit amount of users that can login into an application. Now I've seen people mentioning cookie-based checkups to see how many users are logged into the account but I haven't seen any implementation where I could see how that works exactly.

Besides that I'm wondering if there is already implemented solution of this in .NET MVC 5?

My final goal is following:

Can someone help me out with this one ?

Best regards

Upvotes: 0

Views: 1465

Answers (2)

Manoj Choudhari
Manoj Choudhari

Reputation: 5634

This check sounds similar to Netflix check - you can login using only 5 devices. But there is limitation on devices - hence different sessions in same login ID can be identified using IP addresses and device information in HTTP packet.

This is very nice code sample.

IsUserLoggedInElsewhere will check the logins from other places.

Instead of log everyone else out you will have to log out the first user based on login time whenever third user logs in using same account.

Please refer this article for more details about this.

public static bool IsYourLoginStillTrue(string userId, string sid)
{
    CapWorxQuikCapContext context = new CapWorxQuikCapContext();

    IEnumerable<Logins> logins = (from i in context.Logins
                                    where i.LoggedIn == true && 
                                    i.UserId == userId && i.SessionId == sid
                                    select i).AsEnumerable();
    return logins.Any();
}

public static bool IsUserLoggedOnElsewhere(string userId, string sid)
{
    CapWorxQuikCapContext context = new CapWorxQuikCapContext();

    IEnumerable<Logins> logins = (from i in context.Logins
                                    where i.LoggedIn == true && 
                                    i.UserId == userId && i.SessionId != sid
                                    select i).AsEnumerable();
    return logins.Any();
}

public static void LogEveryoneElseOut(string userId, string sid)
{
    CapWorxQuikCapContext context = new CapWorxQuikCapContext();

    IEnumerable<Logins> logins = (from i in context.Logins 
                                    where i.LoggedIn == true && 
                                    i.UserId == userId && 
                                    i.SessionId != sid // need to filter by user ID
                                    select i).AsEnumerable();

    foreach (Logins item in logins)
    {
        item.LoggedIn = false;
    }

    context.SaveChanges();
}

Upvotes: 1

Sameh Amein
Sameh Amein

Reputation: 11

I think it can be done by one of two ways:

1 : by data base -- Add a field in users table refer to login_status (Bool)- and Last_login_Time (Date) -- Change login_status to (True) and Last_login_Time to dateTime.now -- Before login get from Users table number of users with login_status true -- if count less than two ..normal login -- if count more than = 2 end session for user with earlier login time and set current user is logged..

2 - Also it can be done by using global variables in Global.asax and

Upvotes: 1

Related Questions