FormsAuthenticationModule在哪里注册?

mtk*_*nko 3 asp.net authentication asp.net-mvc

现在我试图找出asp.net mvc身份验证的详细信息.据我所知,FormsAuthenticationModule检查cookie并填写HttpContext.User.但是我无法找到为我的应用程序注册FormsAuthenticationModule的位置?

Dar*_*rov 5

但是我找不到为我的应用程序注册 FormsAuthenticationModule 的位置?

当您<authentication mode="Forms">在 web.config 中设置时,它会由 ASP.NET 运行时自动注册。

如果您对细节感兴趣,您可以查看 ASP.NET 的源代码,更具体地说,如果您在 web.config 中注册了 FormsAuthentication 模块的方法和调用FormsAuthentication 模块方法的HttpApplication类和InitModulesCommon私有方法Init

FormsAuthentication 模块本身,一旦注册,就会订阅AuthenticateRequestHTTP 处理管道的事件,它会尝试根据请求中发送的表单身份验证 cookie 中存在的值将 IPrincipal 构建到当前的 HttpContext 中。

  • 它从浏览器针对每个请求发送的表单身份验证 cookie 中读取它。当您使用 `FormsAuthentication.SetAuthCookie` 静态方法发出表单身份验证 cookie 时,这将使用 cookie 中的机器密钥加密用户名。此 cookie 将在每个后续请求中发送,Forms 身份验证模块将检查其是否存在,尝试对其进行解密,如果成功,它将填充 HttpContext.User 属性。请阅读以下文章 http://msdn.microsoft.com/en-us/library/ff647070.aspx,其中详细描述了整个流程。 (2认同)

Use*_*rol 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>
Run Code Online (Sandbox Code Playgroud)

ASP.NET将它与web.config在文件系统上的层次结构中找到的所有文件合并,因此应用程序默认启用所有模块.