Get a list of all active sessions in ASP.NET

前端 未结 2 563
伪装坚强ぢ
伪装坚强ぢ 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 getOnlineUsers()
        {
            List activeSessions = new List();
            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;
        }
    

提交回复
热议问题