我有一个 MVC5 应用程序,它在生产服务器使用时[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
我可以通过设置来修复它
<modules runAllManagedModulesForAllRequests="true">
但是,我不喜欢使用这样的大锤法 http://www.britishdeveloper.co.uk/2010/06/dont-use-modules-runallmanagedmodulesfo.html.
有没有更干净的方法来解决这个问题?
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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)