Boo*_*Boo 3 .net asp.net asp.net-mvc-3
我存储在HttpContext.Current.Session当前用户中,SiteUser是单音类,它呈现当前活动的siteuser,当他记录我在控制器中创建新的SiteUser()并在构造函数中将他添加到会话中:
public static SiteUser Create()
{
HttpContext.Current.Session[sessionKey] = new SiteUser();
return HttpContext.Current.Session[sessionKey] as SiteUser;
}
Run Code Online (Sandbox Code Playgroud)
然后,对于服务器服务的每个请求,我检查用户是否在会话中可用:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
否则我将生成非auth用户异常并将其重定向到登录页面.但有时HttpContext.Current.Session [sessionKey]为null,但HttpContext.Current.Session不为null且FormsAuthenticationTicket可用且Expired属性也为false.有人可以帮助我,为什么HttpContext.Current.Session [sessionKey]可以为空?
UPD:webconfig会话设置:<sessionState mode="InProc" timeout="20" />
UPD:sessionKey是:public const string sessionKey = "SiteUser";
UPD2:我忘了添加,在会话中还存储了文化设置:
HttpContext.Current.Session["Culture"]
Run Code Online (Sandbox Code Playgroud)
当异常命中时,HttpContext.Current.Session [sessionKey]为null,culture-item不是
UPD3:我已下载源.NET Framework的符号表,并在更改集合项时在SessionStateItemCollection上设置断点.我解决了一些错误:1)所有的收集项都是空的 - "文化"正在设置2)它发生在会话结束事件我无法理解它是怎么回事,因为在web.config会话超时设置为20
sessionKey可能正在改变,你可能只需要做:
HttpContext.Current.Session["CurrentUser"]
Run Code Online (Sandbox Code Playgroud)
或者会话可能即将到期,检查超时:
http://msdn.microsoft.com/en-us/library/h6bb9cz9(VS.71).aspx
或者您可能正在从其他位置设置会话值,通常我通过一个属性控制对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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17637 次 |
| 最近记录: |