c# - 静态 session 类和多用户

标签 c# asp.net session

我正在构建一个类来在 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; }
        }

}

最佳答案

您发布的代码与我们这里的一些代码完全相同。

它已经运行了 2 年了。

每个用户访问都是自己的 session 。向服务器发出的每个请求都是一个新线程。即使同时有 2 个请求,HttpContext.Current 对于每个请求都是不同的。

关于c# - 静态 session 类和多用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1446494/

相关文章:

java - 创建包含用户特定数据的 WebSocket session 的最安全方法

c# - StatusStrip 中的标 checkout 现问题

javascript - 从 iframe 打印 pdf 文件

javascript - 确认删除窗口-点击删除或取消时删除记录

javascript - 如何在 Telerik 包含他们的 javascript 后包含我的 javascript 文件

PHP crypt()、更新和比较

c# - 以编程方式创建 SQL 数据库

c# - VSTO 合并单元格

c# - 后台线程 C#

session - 在 session 中存储数据有哪些风险?