2

This blog post https://weblogs.asp.net/imranbaloch/reading-all-users-session shows how to read all the sessions in an application in asp.net by using Reflection. I'm trying to implement this in my application but getting an error when trying to get _caches field from CacheInternal object. This CacheInternal is giving a CacheMultiple object as you can see in the screen shot;
enter image description here Someone pointed this problem in the comments of the blog post as;
Blog Post Comment But I am not getting collection of CacheSingle objects from the CacheInternal. I'm using Visual Studio 2010 as Administrator and the application is running in FullTrust under securityPolicy. Here is the code taken from the referenced blog;

 private string GetAllInProcSessions(){
    StringBuilder liveSessions = new StringBuilder();
    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 | indingFlags.Instance).GetValue(entry.Value, 
          null);
          if (o1.GetType().ToString() ==      
              "System.Web.SessionState.InProcSessionState")
              {
                  SessionStateItemCollection session = 
                  (SessionStateItemCollection) o1.GetType() 
                   .GetField("_sessionItems",
                    BindingFlags.NonPublic | BindingFlags.Instance)
                    .GetValue(o1);
                    if (session != null)
                    {
                        var lastActivity = session["LastActivity"] != null ? 
                        (DateTime) session["LastActivity"] : DateTime.Now;
                         //liveSessions.Append(...)
                     }
              }
       }
    }
    return liveSessions.ToString();
}
c-sharp
  • 573
  • 1
  • 9
  • 25
  • I think the following will provide an up-to-date solution: https://stackoverflow.com/a/60273007/1086134 – Martin_W Feb 18 '20 at 02:40

0 Answers0