加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

forms-authentication – ASP.NET MVC5应用程序在授权时抛出Null

发布时间:2020-12-16 06:33:13 所属栏目:asp.Net 来源:网络整理
导读:我有一个MVC5应用程序在使用控制器上的[Authorize]属性时在生产服务器上抛出NullReferenceException.该应用程序正在使用表单身份验 生产服务器是Server 2008 SP 2(.NET 4.5.1和IIS 7). 堆栈跟踪的开始是: [NullReferenceException: Object reference not se
我有一个MVC5应用程序在使用控制器上的[Authorize]属性时在生产服务器上抛出NullReferenceException.该应用程序正在使用表单身份验

生产服务器是Server 2008 SP 2(.NET 4.5.1和IIS 7).

堆栈跟踪的开始是:

[NullReferenceException: Object reference not set to an instance of an object.]
   System.Web.Mvc.AuthorizeAttribute.AuthorizeCore(HttpContextBase httpContext) +38
   System.Web.Mvc.AuthorizeAttribute.OnAuthorization(AuthorizationContext filterContext) +293
   System.Web.Mvc.ControllerActionInvoker.InvokeAuthorizationFilters(ControllerContext controllerContext,IList`1 filters,ActionDescriptor actionDescriptor) +155

我可以通过设置来修复它

<modules runAllManagedModulesForAllRequests="true">

但是,我不想使用这样的sledgehammer method.

有没有更清洁的方法来解决这个问题?

解决方法

IIS和IIS Express对请求身份验证有一些不同的行为.由于验证模块并不总是运行,因此当AuthorizeAttribute.AuthorizeCore()方法执行时(因此NullReferenceException),可能无法设置HttpContext.User.Identity属性.

您可以仅更改所需的身份验证模块的前提条件,而不是为所有请求加载所有模块.例如,FormsAuthenticationModule默认具有:preCondition =“managedHandler”.

<system.webServer>
  <modules runAllManagedModulesForAllRequests="false">
    <remove name="FormsAuthentication" />
    <remove name="DefaultAuthentication" />
    <add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" preCondition="" />
    <add name="DefaultAuthentication" type="System.Web.Security.DefaultAuthenticationModule" preCondition="" />
  </modules>
</system.webServer>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读