Get a list of all active sessions in ASP.NET

前端 未结 2 446
伪装坚强ぢ
伪装坚强ぢ 2020-11-28 06:24

I know what user is logged in with the following line of code:

Session[\"loggedInUserId\"] = userId;

The question I have is how do I know w

相关标签:
2条回答
  • 2020-11-28 06:55

    I didn't try rangitatanz solution, but I used another method and it worked just fine for me.

    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;
        }
    
    0 讨论(0)
  • 2020-11-28 07:07

    There is a solution listed in this page List all active ASP.NET Sessions

    private static List<string> _sessionInfo;
    private static readonly object padlock = new object();
    
    public static List<string> Sessions
    {
            get
            {
                lock (padlock)
                {
                    if (_sessionInfo == null)
                    {
                        _sessionInfo = new List<string>();
                    }
                    return _sessionInfo;
                }
            }
      }
    
        protected void Session_Start(object sender, EventArgs e)
        {
            Sessions.Add(Session.SessionID);
        }
        protected void Session_End(object sender, EventArgs e)
        {
            Sessions.Remove(Session.SessionID);
        }
    

    Basically it just tracks sessions into a List that you can use to find out information about. Can really store anything into that that you really want to - Usernames or whatever.

    I don't htink there is anything at the ASP .net layer that does this already?

    0 讨论(0)
提交回复
热议问题