我有一个asp.net MVC应用程序,使用框架4.6,我创建了一个自定义AuthorizeAttribute . 在我的类中,我重写AuthorizeCore方法,并访问会话以验证一些身份验证信息 . 但有时当我尝试在AuthorizeCore方法中访问属性会话时,我有一个NullReferenceException,因为session属性为null .
问题是间歇性的,当我在同一个操作中同时访问时会发生这种情况 . 如果我打开页面并多次刷新页面(浏览器中的F5),则会显示错误 .
我注意到当会话为null时,处理程序也为null .
我正在使用StateServer模式来存储我的会话 . 我尝试更改为InProc模式,我有同样的问题 . 我不知道它是否有所不同,但我使用的是Ninject MVC .
我的自定义属性:
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContextBase)
{
if (!httpContextBase.User.Identity.IsAuthenticated)
{
return false;
}
var infoUser = httpContextBase.Session["infoUser"];
...
}
}
我的控制器:
public class HomeController
{
[CustomAuthorize]
[OutputCache(CacheProfile = CACHE_PRINCIPAL)]
public ActionResult Index()
{
return View();
}
}
1 回答
请查看有关成员资格提供程序,会话提供程序和缓存的此解释:
Authorization and ASP.NET MVC Caching
你使用缓存吗?