2013-11-23 59 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文件合併它發現沿層次文件系統上,因此應用程序默認啓用的所有模塊。

相關問題