[ACCEPTED]-List all active ASP.NET Sessions-session-state
You can collect data about sessions in global.asax 6 events Session_Start and Session_End (only 5 in in-proc settings):
private static readonly List<string> _sessions = new List<string>();
private static readonly object padlock = new object();
public static List<string> Sessions
{
get
{
return _sessions;
}
}
protected void Session_Start(object sender, EventArgs e)
{
lock (padlock)
{
_sessions.Add(Session.SessionID);
}
}
protected void Session_End(object sender, EventArgs e)
{
lock (padlock)
{
_sessions.Remove(Session.SessionID);
}
}
You should consider 4 use some of concurrent collections to lower 3 the synchronization overhead. ConcurrentBag 2 or ConcurrentDictionary. Or ImmutableList 1
https://msdn.microsoft.com/en-us/library/dd997373(v=vs.110).aspx
http://weblogs.asp.net/imranbaloch/archive/2010/04/05/reading-all-users-session.aspx
How this works :
InProc session data is stored in the HttpRuntime’s 16 internal cache in an implementation of ISessionStateItemCollection 15 that implements ICollection. In this code, first 14 of all i got CacheInternal Static Property 13 of HttpRuntime class and then with the help 12 of this object i got _entries private member 11 which is of type ICollection. Then simply 10 enumerate this dictionary and only take 9 object of type System.Web.SessionState.InProcSessionState 8 and finaly got SessionStateItemCollection 7 for each user.
Summary :
In this article, I show you 6 how you can get all current user Sessions. However 5 one thing you will find when executing this 4 code is that it will not show the current 3 user Session which is set in the current 2 request context because Session will be 1 saved after all the Page Events...
It does not seems right that there is not 11 any class or method that provides this information. I 10 think, it is a nice to have feature for 9 SessionStateStoreProvider, to have a method 8 which returns current active session, so 7 that we don't have to actively track session 6 life in session_start and session_end as 5 mention by Jan Remunda.
Since I could not 4 find any out of box method to get all session 3 list, And I did not wanted to track session 2 life as mentioned by Jan, I end up with 1 this solution, which worked in my case.
public static IEnumerable<SessionStateItemCollection> GetActiveSessions()
{
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)
{
yield return sess;
}
}
}
}
}
I've been looking around for an equivalent 7 of @ajitdh's answer for later versions of 6 ASP.net - couldn't find anything, so thought 5 I'd update this thread with solution for 4 v4.6.2... Haven't tested with later versions 3 of .net, but it doesn't work with v4.5. I 2 am guessing it will be compatible with v4.6.1 1 onward.
Cache cache = HttpRuntime.Cache;
MethodInfo method = typeof( System.Web.Caching.Cache ).GetMethod( "GetInternalCache", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy );
object objx = method.Invoke( cache, new object[] { false } );
FieldInfo field = objx.GetType().GetField( "_cacheInternal", BindingFlags.NonPublic | BindingFlags.FlattenHierarchy | BindingFlags.Instance );
objx = field.GetValue( objx );
field = objx.GetType().GetField( "_cachesRefs", BindingFlags.NonPublic | BindingFlags.FlattenHierarchy | BindingFlags.Instance );
objx = field.GetValue( objx );
IList cacherefs = ( (IList)objx );
foreach( object cacheref in cacherefs )
{
PropertyInfo prop = cacheref.GetType().GetProperty( "Target", BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Instance );
object y = prop.GetValue( cacheref );
field = y.GetType().GetField( "_entries", BindingFlags.NonPublic | BindingFlags.FlattenHierarchy | BindingFlags.Instance );
Hashtable c2 = (Hashtable)field.GetValue( y );
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 )
{
// Do your stuff with the session!
}
}
}
}
I really like ajitdh's answer. Do upvote 12 him. Here's another reference to that solution:
http://weblogs.asp.net/imranbaloch/reading-all-users-session
This 11 got me close, but it failed to achieve my 10 personal goal of finding the session for a particular session Id that I knew. So, for 9 my purposes, I just added the sessionid 8 as a session item (say Session["SessionId"] = session.SessionId 7 on Session Start.) Then I just looked for 6 a session with a matching value... I would 5 have preferred to actually pull out this 4 entry by indexing into one of the collections, but 3 this did get it working at least.
Of course, this 2 is all for In-Proc sessions, which I am 1 indeed considering moving away from.
private static SessionStateItemCollection GetSession(string sessionId)
{
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 o0 = entry.Value.GetType().GetProperty("Value", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(entry.Key, null);
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["SessionId"] != null && ((string)sess["SessionId"]) == sessionId)
{
return sess;
}
}
}
}
}
return null;
}
Here's a WebForms/Identity example that 10 gets a list of the logged on users active 9 within the last 30 minutes.
The answer below 8 works for a single web server, and the cache 7 will be lost if the application restarts. If 6 you want to persist the data and share it 5 between servers in a web farm, this answer might be 4 of interest.
In Global.asa.cs:
public static ActiveUsersCache ActiveUsersCache { get; } = new ActiveUsersCache();
and
protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
if (User != null && User.Identity.IsAuthenticated)
{
// Only update when the request is for an .aspx page
if (Context.Handler is System.Web.UI.Page)
{
ActiveUsersCache.AddOrUpdate(User.Identity.Name);
}
}
}
The add 3 these couple of classes:
public class ActiveUsersCache
{
private readonly object padlock = new object();
private readonly Dictionary<string, DateTime> cache = new Dictionary<string, DateTime>();
private DateTime lastCleanedAt;
public int ActivePeriodInMinutes { get; } = 30;
private const int MinutesBetweenCacheClean = 30;
public List<ActiveUser> GetActiveUsers()
{
CleanCache();
var result = new List<ActiveUser>();
lock (padlock)
{
result.AddRange(cache.Select(activeUser => new ActiveUser {Name = activeUser.Key, LastActive = activeUser.Value}));
}
return result;
}
private void CleanCache()
{
lastCleanedAt = DateTime.Now;
var cutoffTime = DateTime.Now - TimeSpan.FromMinutes(ActivePeriodInMinutes);
lock (padlock)
{
var expiredNames = cache.Where(au => au.Value < cutoffTime).Select(au => au.Key).ToList();
foreach (var name in expiredNames)
{
cache.Remove(name);
}
}
}
public void AddOrUpdate(string userName)
{
lock (padlock)
{
cache[userName] = DateTime.Now;
}
if (IsTimeForCacheCleaup())
{
CleanCache();
}
}
private bool IsTimeForCacheCleaup()
{
return lastCleanedAt < DateTime.Now - TimeSpan.FromMinutes(MinutesBetweenCacheClean);
}
}
public class ActiveUser
{
public string Name { get; set; }
public DateTime LastActive { get; set; }
public string LastActiveDescription
{
get
{
var timeSpan = DateTime.Now - LastActive;
if (timeSpan.Minutes == 0 && timeSpan.Seconds == 0)
return "Just now";
if (timeSpan.Minutes == 0)
return timeSpan.Seconds + "s ago";
return $"{timeSpan.Minutes}m {timeSpan.Seconds}s ago";
}
}
}
Finally, in the 2 page where you want to display the active 1 users, display the results:
UserRepeater.DataSource = Global
.ActiveUsersCache.GetActiveUsers().OrderByDescending(u => u.LastActive);
UserRepeater.DataBind();
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.