静态会话类和多个用户
我正在构建一个类来在会话中存储用户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包装两个会话状态。
- 我应该永远不会调用HostingEnvironment.UnregisterObject吗?
- CngKey.Import(…); 在PC上成功但在Web主机上失败
- VB等效的C#事件创建
- 在c#中输出一个受控的QueryString
- 如何获取DataSet.ReadXml以将DateTime属性解析为类型化DateTime
- 如何从Request.Form获取所有元素值,而无需使用.GetValues(“ElementIdName”)确切指定哪个元素值
- 无法加载文件或程序集”或其依赖项之一
- 更新映像而不会闪烁ASP.NET C#
- C#如何将System.Net.ConnectStream转换为byte (数组)