Click here to Skip to main content
15,900,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to maintain one session per user in my asp.net application i.e. to stop multiple logins by a single user at the same time.
What is the best solution for this? Please help.
Posted

C#
private List<string> getOnlineUsers()
    {
        List<string> activeSessions = new List<string>();
        object obj = typeof(HttpRuntime).GetProperty("CacheInternal", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null, null);
        object[] obj2 = (object[])obj.GetType().GetField("_caches", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(obj);
        for (int i = 0; i < obj2.Length; i++)
        {
            Hashtable c2 = (Hashtable)obj2[i].GetType().GetField("_entries", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(obj2[i]);
            foreach (DictionaryEntry entry in c2)
            {
                object o1 = entry.Value.GetType().GetProperty("Value", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(entry.Value, null);
                if (o1.GetType().ToString() == "System.Web.SessionState.InProcSessionState")
                {
                    SessionStateItemCollection sess = (SessionStateItemCollection)o1.GetType().GetField("_sessionItems", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(o1);
                    if (sess != null)
                    {
                        if (sess["loggedInUserId"] != null)
                        {
                            activeSessions.Add(sess["loggedInUserId"].ToString());
                        }
                    }
                }
            }
        }
        return activeSessions;
    }</string></string></string>


by using this you can get the list of all active sessions ,so when the user gets login try to check ,user is already present in the list or not.

Reference:
http://stackoverflow.com/questions/8854176/get-a-list-of-all-active-sessions-in-asp-net[^]
 
Share this answer
 
Hi,

There is no "configuration setting" for this, you have to build it yourself.
You need to maintain a global list of "logged in users". If someone tries
to log in, check against this list and display a "already logged in"
message.
You need to add code to the Session_End event handler to remove
users from this list if their session expires (when they just left the site/closed
their browser instead of logging out).

Be careful: users that had a browser-crash that prevented them from logging out,
now need to wait 20 minutes before they can access your site again!
The fact that your user is logged in in some session that he/she can't log out
of, doesn't have to be his/her fault!

Regards ,
Nirav Prabtani.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900