get session ID in ASP.Net

后端 未结 6 1614
滥情空心
滥情空心 2021-02-02 07:31

how can I get IDs of all current sessions?

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-02 08:22

    If you want a way to store a list of the current sessions where you control the backing store, so that you may store extra data about the client, you can use a list. (I'm writing the following example from the top of my head)

    Hook into Application_SessionStart in the global.asax.cs file:

    static List sessions = new List();
    static object sessionLock = new object();
    
    void Application_SessionStart()
    {
        lock (sessionLock) {
            sessions.Add(Session.SessionID);
        }
    }
    
    void Application_SessionEnd()
    {
        lock (sessionLock) {
            sessions.Remove(Session.SessionID);
        }
    }
    

    Alternatively, you can use a dictionary, storing the session ID as a key, and extra data about that user as the value. Then you can easily create a page that shows all current user sessions, for example, for an admin site to show current user sessions.

    SessionEnd will only be called if your sessions are InProc.

提交回复
热议问题