【问题标题】:Session is null in Custom AuthorizeAttribute [closed]自定义 AuthorizeAttribute 中的会话为空 [关闭]
【发布时间】:2016-02-11 19:27:40
【问题描述】:

我有一个使用框架 4.6 的 asp.net MVC 应用程序,并创建了一个自定义 AuthorizeAttribute。在我的课程中,我重写了 AuthorizeCore 方法并访问会话以验证一些身份验证信息。但有时当我尝试在 AuthorizeCore 方法中访问属性会话时,我会遇到 NullReferenceException,因为会话属性为空。

该问题是间歇性的,当我在同一操作中同时访问时会发生此问题。如果我打开页面并多次刷新页面(浏览器中的 F5)它会显示错误。

我注意到当会话为空时,处理程序也为空。

我正在使用 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();
        }
}

【问题讨论】:

    标签: c# asp.net asp.net-mvc session


    【解决方案1】:

    请看一下关于会员提供者、会话提供者和缓存的解释:

    Authorization and ASP.NET MVC Caching

    你使用缓存吗?

    【讨论】:

    • 是的,我使用的是 OutputCache。我删除了,它工作正常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-18
    • 1970-01-01
    • 1970-01-01
    • 2018-01-15
    相关资源
    最近更新 更多