现在我正在尝试详细了解asp.net mvc身份验证的工作原理。据我所知,FormsAuthenticationModule会检查cookie并填充HttpContext.User。但是我找不到在我的应用程序中注册FormsAuthenticationModule的地方?
现在我正在尝试详细了解asp.net mvc身份验证的工作原理。据我所知,FormsAuthenticationModule会检查cookie并填充HttpContext.User。但是我找不到在我的应用程序中注册FormsAuthenticationModule的地方?
它继承自根 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
文件合并,以便应用程序默认启用所有模块。
FormsAuthentication.SetAuthCookie
静态方法发出Forms身份验证cookie时,它将使用cookie中的机器密钥加密用户名。此cookie将在每个后续请求中发送,Forms身份验证模块将检查其是否存在,尝试解密它,如果成功,它将填充HttpContext.User属性。请阅读以下文章http://msdn.microsoft.com/en-us/library/ff647070.aspx,其中详细描述了整个流程。 - Darin Dimitrov