2013-11-23 60 views
3

现在我试图找出如何asp.net mvc身份验证的细节。据我所知,完全FormsAuthenticationModule检查cookie并填充HttpContext.User。但我无法找到FormsAuthenticationModule为我的应用程序注册的位置?FormsAuthenticationModule在哪里注册?

+1

它已在Web服务器级别上注册。假设你在IIS上运行,转到IIS管理器,选择顶层并单击模块,然后你会发现它 – Uriil

+0

并且依赖于认证类型(窗口或窗体)iis调用适当的认证模块(WindowsAuthenticationModule或FormsAuthenticationModule)? – mtkachenko

回答

4

但我无法找到FormsAuthenticationModule在哪里注册了我的应用程序?

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

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

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

+0

在应用程序成员资格提供程序中使用formsauthenticationmodule注册以获取有关用户的数据吗? – mtkachenko

+0

不,表单身份验证与成员资格提供程序完全无关。那些是可以独立使用的两种完全不同的东西。 –

+0

模块如何获取用户名? – mtkachenko

5

它从根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文件合并它发现沿层次文件系统上,因此应用程序默认启用的所有模块。

相关问题