gpt4 book ai didi

.net - HttpContext.Current.Session 空项目

转载 作者:行者123 更新时间:2023-12-02 17:38:19 26 4
gpt4 key购买 nike

我存储在 HttpContext.Current.Session 当前用户中,SiteUser 是显示当前事件站点用户的单音类,当他登录时,我在 Controller 和构造函数中创建新的 SiteUser() ,将他添加到 session 中:

public static SiteUser Create()
{
HttpContext.Current.Session[sessionKey] = new SiteUser();

return HttpContext.Current.Session[sessionKey] as SiteUser;
}

然后,对于对服务器服务的每个请求,我都会检查用户在 session 中是否可用:

public static SiteUser Current
{
get
{
if (HttpContext.Current.Session == null || HttpContext.Current.Session[sessionKey] == null)
{
throw new SiteUserAutorizationExeption();
}

return HttpContext.Current.Session[sessionKey] as SiteUser;
}
}

否则我将生成非身份验证用户异常并将其重定向到登录页面。但有时 HttpContext.Current.Session[sessionKey] 为 null,但 HttpContext.Current.Session 不为 null 并且 FormsAuthenticationTicket 可用且 Expired 属性也为 false。有人可以帮助我,为什么 HttpContext.Current.Session[sessionKey] 可以为 null?

UPD:webconfig session 设置:<sessionState mode="InProc" timeout="20" />

UPD: session key 是:public const string sessionKey = "SiteUser";

UPD2:我忘记添加,在 session 中还存储了文化设置:

HttpContext.Current.Session["Culture"]

当异常发生时,HttpContext.Current.Session[sessionKey]为空,culture-item不是

UPD3:我已下载源 .NET Framework 的符号表,并在更改集合项时在 SessionStateItemCollection 处设置断点。我解决了一些错误:1) 所有集合项均为空——“文化”是在之后设置的2)它发生在 session 结束事件时我不明白这是怎么回事,因为在 web.config session 超时设置为 20

最佳答案

sessionKey 可能会发生变化,您可能只需要执行以下操作:

HttpContext.Current.Session["CurrentUser"]

或者 session 可能即将过期,请检查超时:

http://msdn.microsoft.com/en-us/library/h6bb9cz9(VS.71).aspx

或者您可能从其他地方设置 session 值,通常我通过一个属性控制对 Session/Context 对象的访问

static readonly string SESSION_CurrentUser = "CurrentUser";

public static SiteUser Create() {
SiteUser.Current = new SiteUser();
return SiteUser.Current;
}

public static SiteUser Current {
get {
if (HttpContext.Current.Session == null || HttpContext.Current.Session[SESSION_CurrentUser] == null) {
throw new SiteUserAutorizationExeption();
}
return HttpContext.Current.Session[SESSION_CurrentUser] as SiteUser;
}
set {
if (!HttpContext.Current.Session == null) {
HttpContext.Current.Session[SESSION_CurrentUser] = value;
}
}
}

关于.net - HttpContext.Current.Session 空项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4710411/

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