Alex Alex - 2 months ago 29
ASP.NET (C#) Question

List all active ASP.NET Sessions

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

Answer

Iterating through all sessions will be probably not very thread-safe. But you can collect informations about sessions in global.asax evets Session_Start and Session_End:

private static List<string> _sessionInfo;
private static readonly object padlock = new object();

 public static List<string> Sessions
 {
            get
            {
                lock (padlock)
                {
                    if (_sessionInfo == null)
                    {
                        _sessionInfo = new List<string>();
                    }
                    return _sessionInfo;
                }
            }
  }

        protected void Session_Start(object sender, EventArgs e)
        {
            Sessions.Add(Session.SessionID);
        }
        protected void Session_End(object sender, EventArgs e)
        {
            Sessions.Remove(Session.SessionID);
        }
Comments