How to view everything in session state for all active sessions?

I would like to create an administrative page to show that our use of session state is not out of control.

Is it possible to get a list of all active sessions, and if so, how can I access all the session data in each session?

+3
source share
2 answers

Disclaimer: I just came up with this implementation because I thought it was an interesting and solvable problem. Thus, there may be some problems or details that I did not take into account. However, if you are using InProc session state, here is the solution.

: (, ), , Application_Start, Session_Start . Session_End . , , .

Global.asax

void Application_Start(object sender, EventArgs e) 
{
    Application["activeSessions"] = new System.Collections.Generic.List<HttpSessionState>();
}

void Session_Start(object sender, EventArgs e) 
{
    var activeSessions = (System.Collections.Generic.List<HttpSessionState>)Application["activeSessions"];
    activeSessions.Add(this.Session);
}

void Session_End(object sender, EventArgs e) 
{
    var activeSessions = (System.Collections.Generic.List<HttpSessionState>)Application["activeSessions"];
    activeSessions.Remove(this.Session);
}

SomePage.aspx

    //add something to session for test
    this.Session["someStr"] = DateTime.Now.ToString();

    //get sessions
    var activeSessions = (List<HttpSessionState>)Application["activeSessions"];
    foreach (var session in activeSessions)
    {
        Response.Write("Session " + session.SessionID + "<br/>");
        foreach (string key in session.Keys)
        {
            Response.Write(key + " : " + session[key] + "<br/>");
        }
        Response.Write("<hr/>");
    }

: ( )

Session sj0sa255uizwlu45zivyfg2m 
someStr : 8/28/2009 11:03:37 AM
----
Session 530b3sjtea22jm451p15u355 
someStr : 8/28/2009 11:03:43 AM
----
+4

. , , , . , , .

"" / db, , .

0

Source: https://habr.com/ru/post/1716311/


All Articles