I have implemented the code for reading the active sessions using the reference Reading All Users Session and Get a list of all active sessions in ASP.NET.
P
Try using this, _cachesRefs if _caches is NULL. The function below will return all user sessions collections for all multiple versions of Windows and including Windows Server.
It works.
public List GetAllUserSessions() {
List hTables = new List();
PropertyInfo propInfo = typeof(HttpRuntime).GetProperty("CacheInternal", BindingFlags.NonPublic | BindingFlags.Static);
object CacheInternal = propInfo.GetValue(null, null);
dynamic fieldInfo = CacheInternal.GetType().GetField("_caches", BindingFlags.NonPublic | BindingFlags.Instance);
if (fieldInfo != null) {
object[] _caches = (object[])fieldInfo.GetValue(CacheInternal);
for (int i = 0; i <= _caches.Length - 1; i++) {
Hashtable hTable = (Hashtable)_caches(i).GetType().GetField("_entries", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(_caches(i));
hTables.Add(hTable);
}
} else {
fieldInfo = CacheInternal.GetType().GetField("_cachesRefs", BindingFlags.NonPublic | BindingFlags.Instance);
dynamic cacheRefs = fieldInfo.GetValue(CacheInternal);
foreach (void cacheRef_loopVariable in cacheRefs) {
cacheRef = cacheRef_loopVariable;
dynamic target = cacheRef.Target;
fieldInfo = target.GetType().GetField("_entries", BindingFlags.NonPublic | BindingFlags.Instance);
Hashtable hTable = fieldInfo.GetValue(target);
hTables.Add(hTable);
}
}
List sessionlist = new List();
foreach (void hTable_loopVariable in hTables) {
hTable = hTable_loopVariable;
foreach (DictionaryEntry entry in hTable) {
object value = entry.Value.GetType().GetProperty("Value", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(entry.Value, null);
if (value.GetType().ToString() == "System.Web.SessionState.InProcSessionState") {
SessionStateItemCollection sCollection = (SessionStateItemCollection)value.GetType().GetField("_sessionItems", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(value);
if (sCollection != null)
sessionlist.Add(sCollection);
}
}
}
return sessionlist;
}