静态会话类和多个用户

Ton*_*orf 7 c# asp.net session

我正在构建一个类来在会话中存储用户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; }
        }

}
Run Code Online (Sandbox Code Playgroud)

Jea*_*ois 6

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

它已经工作了2年了.

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