get session ID in ASP.Net

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

how can I get IDs of all current sessions?

相关标签:
6条回答
  • 2021-02-02 08:16

    To get the session id, do this:

    // In a user control or page
    string sessionId = this.Session.SessionID; 
    
    // In a normal class, running in a asp.net app.
    string sessionId = System.Web.HttpContext.Current.Session.SessionID; 
    

    You should not need to:

    • Make any data table or loop anything
    • Use SQL server for session state
    • Handle Session_Start or Session_End

    In a cookieless scenario, the session id is created when you access the Session object for the first time. This shouldn't matter much, because the moment you access the SessionID property, the session object is accessed.

    For more info, look into this:

    http://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.sessionid.aspx

    Note: The msdn examples have been written by monkeys.

    0 讨论(0)
  • 2021-02-02 08:16

    If you are storing your session state in SQL Server, you can also easily get it from there.

    0 讨论(0)
  • 2021-02-02 08:17

    The answer depends partially on where you store session state. Assuming you use the default (inproc) then you can maintain a list of current session ids using the Session_Start and Session_End events in global.asax.

    0 讨论(0)
  • 2021-02-02 08:22

    According to Dino Esposito each session is stored in the application's Cache and with some work you can retreive this information:

    DataTable dt = new DataTable();
    dt.Columns.Add("SessionID", typeof(string));
    foreach(DictionaryEntry elem in Cache) {
        string s = elem.Key.ToString();
        if (s.StartsWith("System.Web.SessionState.SessionStateItem")) {
            DataRow row = dt.NewRow();
            char[] parms = {':'};
            string[] a = s.Split(parms);
            row["SessionID"] = a[1];
            dt.Rows.Add(row);
        }
    }
    
    0 讨论(0)
  • 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<string> sessions = new List<string>();
    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.

    0 讨论(0)
  • 2021-02-02 08:27

    You can use Global.asax file and set the Session at Session_Start event. See below

    in Global.asax file you can do something like this:

    protected void Session_Start(object sender, EventArgs e)
    {
        Session["sid"] = Session.SessionID;
        Session["sid"] = "Test";
    } 
    

    Then in your WebForm you can get the Session ID and Value like below

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("Session ID is:" + Session.SessionID.ToString()+ "<br/>");
        Response.Write("Session value is:" + Session["sid"].ToString());
    } 
    

    For details, see http://www.dotnetcurry.com/ShowArticle.aspx?ID=126

    0 讨论(0)
提交回复
热议问题