gpt4 book ai didi

c# - 静态 session 类和多用户

转载 作者:太空狗 更新时间:2023-10-29 20:15:25 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com