dom*_*mer 4 forms-authentication httpmodule asp.net-mvc-5
我有一个MVC5应用程序,当在控制器上使用该属性时,它会在生产服务器上抛出NullReferenceException [Authorize].该应用程序正在使用表单身份验
生产服务器是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
Run Code Online (Sandbox Code Playgroud)
我可以通过设置来修复它
<modules runAllManagedModulesForAllRequests="true">
Run Code Online (Sandbox Code Playgroud)
但是,我不喜欢使用这种大锤方法.
有没有更清洁的方法来解决这个问题?
小智 7
IIS和IIS Express对请求身份验证有一些不同的行为.由于验证模块并不总是运行,因此HttpContext.User.Identity在AuthorizeAttribute.AuthorizeCore()方法执行时(因此NullReferenceException)可能不会设置该属性.
您可以仅更改所需的身份验证模块的前提条件,而不是为所有请求加载所有模块.例如,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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2274 次 |
| 最近记录: |