如何查看所有活动会话的会话状态中的所有内容?

发布于 2024-08-02 18:20:01 字数 83 浏览 1 评论 0原文

我想创建一个管理页面来表明我们对会话状态的使用并没有失控。

是否可以检索所有活动会话的列表,如果可以,如何访问每个会话中的所有会话数据?

I'd like to create an administrative page to show that our use of session state isn't getting out of hand.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

将军与妓 2024-08-09 18:20:01

免责声明:我只是想出了这个实现,因为我认为这是一个有趣且可以解决的问题。因此,可能有一些问题或细节我忽略了考虑。不过,如果您使用 InProc 会话状态,这里有一个解决方案。

摘要:创建一个应用程序级对象(例如列表),存储在 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/>");
    }

输出:(加载第二个浏览器访问该页面后)

会话 sj0sa255uizwlu45zivyfg2m 
一些Str : 8/28/2009 11:03:37 上午
----
会话 530b3sjtea22jm451p15u355 
一些Str : 8/28/2009 11:03:43 上午
----

Disclaimer: I just came up with this implementation because I thought this was an interesting - and solvable - problem. As such, there may be some issues or details I've neglected to consider. Nevertheless, if you are using InProc session state, here's a solution.

Summary: Create an Application-level object (eg. a List) stored in Application state created during the Application_Start event, and on each Session_Start event, add a reference to the session to your list. On Session_End, remove it. To retrieve all the active sessions and values, loop through your list of sessions, then through the session keys of each.

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/>");
    }

Output: (after loading up a second browser to hit the page)

Session sj0sa255uizwlu45zivyfg2m 
someStr : 8/28/2009 11:03:37 AM
----
Session 530b3sjtea22jm451p15u355 
someStr : 8/28/2009 11:03:43 AM
----
一生独一 2024-08-09 18:20:01

无法从另一个会话访问会话。然而,通过实现会员资格提供程序,您可以知道会话是否处于活动状态以及有关用户活动的许多其他有用信息。此外,通过使用数据库保存会话状态,您可以检索所需的信息。

您可以使用“活动”标志来存储/删除数据库中的会话,以获得更具可扩展性的解决方案,以防这对您很重要。

Session's cant be accessed from another session. However by implementing membership provider you could know if a session is active and many other useful information about user's activities. Also by persisting the session state using a DB you could retrieve the information you want.

You could use an "active" flag to store/remove sessions in the db just to obtain a more scalable solution, in case that's important for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文