Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all active ASP.NET Sessions

How can I list (and iterate through) all current ASP.NET sessions?

like image 844
Alex Avatar asked Sep 24 '09 08:09

Alex


1 Answers

You can collect data about sessions in global.asax events Session_Start and Session_End (only 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 use some of concurrent collections to lower the synchronization overhead. ConcurrentBag or ConcurrentDictionary. Or ImmutableList

https://msdn.microsoft.com/en-us/library/dd997373(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/dn467185.aspx

like image 167
Jan Remunda Avatar answered Sep 21 '22 16:09

Jan Remunda