现在我正在尝试详细了解asp.net mvc身份验证的工作原理。据我所知,FormsAuthenticationModule检查cookie并填写HttpContext.User。但我无法找到为我的应用程序注册FormsAuthenticationModule的位置?
答案 0 :(得分:5)
它继承自root web.config。例如,如果您在x64计算机上安装了.NET 4,请打开C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\web.config
。在system.web
部分,您会找到以下注册的模块:
<httpModules>
<add name="OutputCache" type="System.Web.Caching.OutputCacheModule" />
<add name="Session" type="System.Web.SessionState.SessionStateModule" />
<add name="WindowsAuthentication" type="System.Web.Security.WindowsAuthenticationModule" />
**<add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" />**
<add name="PassportAuthentication" type="System.Web.Security.PassportAuthenticationModule" />
<add name="RoleManager" type="System.Web.Security.RoleManagerModule" />
<add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" />
<add name="FileAuthorization" type="System.Web.Security.FileAuthorizationModule" />
<add name="AnonymousIdentification" type="System.Web.Security.AnonymousIdentificationModule" />
<add name="Profile" type="System.Web.Profile.ProfileModule" />
<add name="ErrorHandlerModule" type="System.Web.Mobile.ErrorHandlerModule, System.Web.Mobile, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<add name="ServiceModel" type="System.ServiceModel.Activation.HttpModule, System.ServiceModel.Activation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" />
<add name="ScriptModule-4.0" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</httpModules>
ASP.NET将它与文件系统层次结构中找到的所有web.config
文件合并,因此应用程序默认启用所有模块。
答案 1 :(得分:4)
但我无法找到为我的应用程序注册FormsAuthenticationModule的位置?
在web.config中设置<authentication mode="Forms">
时,ASP.NET运行时会自动注册它。
如果您对详细信息感兴趣,可以查看ASP.NET的源代码,更具体地说是HttpApplication
类和InitModulesCommon
私有方法,它调用{{1如果您在web.config中注册了FormsAuthentication模块的方法。
FormsAuthentication模块本身一旦注册,将订阅HTTP处理管道的Init
事件,它将尝试根据发送的表单身份验证cookie中的值,在当前的HttpContext中构建IPrincipal。请求。