繁体   English   中英

静态会话类和多个用户

[英]Static Session Class and Multiple Users

我正在构建一个类来在会话中存储用户ID和用户角色。 我不确定当多个用户同时在网站上时这个类会如何表现。 有没有人看到这个问题?

public static class SessionHandler
    {   
        //*** Session String Values ***********************

        private static string _userID = "UserID";
        private static string _userRole = "UserRole";

        //*** Sets and Gets **********************************************************

        public static string UserID
        {
            get
            {
                if (HttpContext.Current.Session[SessionHandler._userID] == null)
                { return string.Empty; }
                else
                { return HttpContext.Current.Session[SessionHandler._userID].ToString(); }
            }
            set
            { HttpContext.Current.Session[SessionHandler._userID] = value; }

        }
        public static string UserRole
        {
            get
            {
                if (HttpContext.Current.Session[SessionHandler._userRole] == null)
                { return string.Empty; }
                else
                { return HttpContext.Current.Session[SessionHandler._userRole].ToString(); }
            }
            set
            { HttpContext.Current.Session[SessionHandler._userRole] = value; }
        }

}

您发布的代码是我们在这里的一些代码的完全复制品。

它已经工作了2年了。

每个用户访问都是自己的会话。 对服务器发出的每个请求都是一个新线程。 尽管2个请求是同时发生的,但每个请求的HttpContext.Current都不同。

您将为每个连接获得一个新会话。 没有两个用户会共享会话。 每个连接都有自己的SessionID值。 只要用户停留在您的页面上(不关闭浏览器等),用户就会将该会话从一个请求保留到下一个请求。

这对于访问您的应用程序的多个用户来说可以正常工作,因为将为所有访问应用程序的不同用户生成不同的sessionid。 如果您在系统中定义了两个不同的会话变量,它将以类似的方式工作。 它就像使用静态包装类SessionHandler包装两个会话状态。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM