<httpModules> <add name="WindowsAuthentication" type="System.Web.Security.WindowsAuthenticationMod只加载一个身份验证模块,这取决于该配置文件的[b]authentication[/b] 元素中指定了哪种身份验证模式。该身份验证模块创建一个[b]IPrincipal[/b] 对象并将它存储在[b]HttpContext.User[/b] 属性中。这是很关键的,因为其他授权模块使用该[b]IPrincipal[/b] 对象作出授权决定。 当 IIS 中启用匿名访问且[b]authentication[/b] 元素的[b]mode[/b] 属性设置为[b]none[/b] 时,有一个特殊模块将默认的匿名原则添加到[b]HttpContext.User[/b] 属性中。因此,在进行身份验证之后,[b]HttpContext.User[/b] 绝不是一个空引用(在 Visual Basic 中为[b]Nothing[/b])。 WindowsAuthenticationModule 如果 Web.config 文件包含以下元素,则激活[b]WindowsAuthenticationModule[/b] 类。ule" /> <add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModul e" /> <add name="PassportAuthentication" type="System.Web.Security.PassportAuthenticationMo dule" /> </httpModules>
<authentication mode="Windows" />[b]WindowsAuthenticationModule[/b] 类负责创建[b]WindowsPrincipal[/b] 和[b]WindowsIdentity[/b] 对象来表示经过身份验证的用户,并且负责将这些对象附加到当前 Web 请求。 对于 Windows 身份验证,遵循以下步骤: [list] [*] [b]WindowsAuthenticationModule[/b] 使用从 IIS 传递到 ASP.NET 的 Windows 访问令牌创建一个[b]WindowsPrincipal[/b] 对象。该令牌包装在[b]HttpContext[/b] 类的[b]WorkerRequest[/b] 属性中。引发[b]AuthenticateRequest[/b] 事件时,[b]WindowsAuthenticationModule[/b] 从[b]HttpContext[/b] 类检索该令牌并创建[b]WindowsPrincipal[/b] 对象。[b]HttpContext.User[/b] 用该[b]WindowsPrincipal[/b] 对象进行设置,它表示所有经过身份验证的模块和 ASP.NET 页的经过身份验证的用户的安全上下文。 [/*][*] [b]WindowsAuthenticationModule[/b] 类使用 P/Invoke 调用 Win32 函数并获得该用户所属的 Windows 组的列表。这些组用于填充[b]WindowsPrincipal[/b] 角色列表。 [/*][*] [b]WindowsAuthenticationModule[/b] 类将[b]WindowsPrincipal[/b] 对象存储在[b]HttpContext.User[/b] 属性中。随后,授权模块用它对经过身份验证的用户授权。 [b]注:[/b][b]DefaultAuthenticationModule[/b] 类(也是 ASP.NET 管道的一部分)将[b]Thread.CurrentPrincipal[/b] 属性设置为与[b]HttpContext.User[/b] 属性相同的值。它在处理[b]AuthenticateRequest[/b] 事件之后进行此操作。 [/*][/list] 授权模块 [b]WindowsAuthenticationModule[/b] 类完成其处理之后,如果未拒绝请求,则调用授权模块。授权模块也在计算机级别的 Web.config 文件中的[b]httpModules[/b] 元素中定义,如下所示:
<httpModules> <add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" /> <add name="FileAuthorization" type="System.Web.Security.FileAuthorizationModule" /> <add name="AnonymousIdentification" type="System.Web.Security.AnonymousIdentificationMUrlAuthorizationModule 调用[b]UrlAuthorizationModule[/b] 类时,它在计算机级别或应用程序特定的 Web.config 文件中查找[b]authorization[/b] 元素。如果存在该元素,则[b]UrlAuthorizationModule[/b] 类从[b]HttpContext.User[/b] 属性检索[b]IPrincipal[/b] 对象,然后使用指定的动词(GET、POST 等)来确定是否授权该用户访问请求的资源。 FileAuthorizationModule 接下来,调用[b]FileAuthorizationModule[/b] 类。它检查[b]HttpContext.User.Identity[/b] 属性中的[b]IIdentity[/b] 对象是否是[b]WindowsIdentity[/b] 类的一个实例。如果[b]IIdentity[/b] 对象不是[b]WindowsIdentity[/b] 类的一个实例,则[b]FileAuthorizationModule[/b] 类停止处理。 如果存在[b]WindowsIdentity[/b] 类的一个实例,则[b]FileAuthorizationModule[/b] 类调用[b]AccessCheck[/b] Win32 函数(通过 P/Invoke)来确定是否授权经过身份验证的客户端访问请求的文件。如果该文件的安全描述符的随机访问控制列表 (DACL) 中至少包含一个[b]Read[/b] 访问控制项 (ACE),则允许该请求继续。否则,[b]FileAuthorizationModule[/b] 类调用[b]HttpApplication.CompleteRequest[/b] 方法并将状态码 401 返回到客户端。 [h2]安全上下文[/h2] .NET Framework 使用以下两个接口封装 Windows 令牌和登录会话: [list] [*] [b]System.Security.Principal.IPrincipal[/b] [/*][*] [b]System.Security.Principal.IIdentity[/b](它公开为[b]IPrincipal[/b] 接口中的一个属性。) [/*][/list] HttpContext.User 在 ASP.NET 中,用[b]WindowsPrincipal[/b] 和[b]WindowsIdentity[/b] 类表示使用 Windows 身份验证进行身份验证的用户的安全上下文。使用 Windows 身份验证的 ASP.NET 应用程序可以通过[b]HttpContext.User[/b] 属性访问[b]WindowsPrincipal[/b] 类。 要检索启动当前请求的 Windows 经过身份验证的用户的安全上下文,使用以下代码:odule" /> </httpModules>
using System.Security.Principal; ... // Obtain the authenticated user's Identity WindowsPrincipal winPrincipal = (WindowsPrincipal)HttpContext.Current.User;WindowsIdentity.GetCurrent [b]WindowsIdentity.GetCurrent[/b] 方法可用于获得当前运行的 Win32 线程的安全上下文的标识。如果不使用模拟,线程继承 IIS 6.0(默认情况下的 NetworkService 帐户)上进程的安全上下文。 该安全上下文在访问本地资源时使用。通过使用经过身份验证的初始用户的安全上下文或使用固定标识,您可以使用模拟重写该安全上下文。 要检索运行应用程序的安全上下文,使用以下代码:
using System.Security.Principal; ... // Obtain the authenticated user's identity. WindowsIdentity winId = WindowsIdentity.GetCurrent(); WindowsPrincipal winPrincipal = new WindowsPrincipal(winId);Thread.CurrentPrincipal ASP.NET 应用程序中的每个线程公开一个[b]CurrentPrincipal[/b] 对象,该对象保存经过身份验证的初始用户的安全上下文。该安全上下文可用于基于角色的授权。 要检索线程的当前原则,使用以下代码:
using System.Security.Principal; ... // Obtain the authenticated user's identity WindowsPrincipal winPrincipal = (WindowsPrincipal) Thread.CurrentPrincipal();表 1 显示从各种标识属性获得的结果标识,当您的应用程序使用 Windows 身份验证且 IIS 配置为使用集成 Windows 身份验证时,可以从 ASP.NET 应用程序使用这些标识属性。 [b]表 1:线程公开的 CurrentPrincipal Object[/b]
| Web.config 设置 | 变量位置 | 结果标识 |
|---|---|---|
| <identity impersonate="true"/> <authentication mode="Windows" /> | HttpContext WindowsIdentity 线程 | DomainUserName DomainUserName DomainUserName |
| <identity impersonate="false"/> <authentication mode="Windows" /> | HttpContext WindowsIdentity 线程 | DomainUserName NT AUTHORITYNETWORK SERVICE DomainUserName |
| <identity impersonate="true"/> <authentication mode="Forms" /> | HttpContext WindowsIdentity 线程 | 用户提供的名称 DomainUserName 用户提供的名称 |
| <identity impersonate="false"/> <authentication mode="Forms" /> | HttpContext WindowsIdentity 线程 | 用户提供的名称 NT AUTHORITYNETWORK SERVICE 用户提供的名称 |
<authentication mode="Windows" /> <identity impersonate="true" />使用该配置,ASP.NET 始终模拟经过身份验证的用户,且所有资源访问均使用经过身份验证的用户的安全上下文执行。如果您的应用程序的虚拟目录上启用了匿名访问,则模拟 IUSR_MACHINENAME 帐户。 要暂时模拟经过身份验证的调用方,将[b]identity[/b] 元素的[b]impersonate[/b] 属性设置为[b]false[/b], 然后使用以下代码:
using System.Security.Principal; ... // Obtain the authenticated user's identity. WindowsIdentity winId = (WindowsIdentity)HttpContext.Current.User.Identity; WindowsImpersonationCont这段代码模拟经过身份验证的初始用户。在[b]HttpContext.Current.User.Identity[/b] 对象中维护初始用户的标识和 Windows 令牌。 固定标识模拟 如果需要在应用程序的整个生命周期中模拟相同的标识,可以在 Web.config 文件中的[b]identity[/b] 元素上指定凭据。以下示例显示如何模拟名为"TestUser"的 Windows 帐户。 如果使用该方法,应该对这些凭据进行加密。使用 ASP.NET 2.0,您可以使用 ASP.NET IIS 注册工具 (Aspnet_regiis.exe)。使用 ASP.NET 1.1 版,您可以使用 Aspnet_setreg.exe 实用工具。有关该实用工具的详细信息,请参阅 [url=http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cptools/html/cpgrfaspnetiisregistrationtoolaspnet_regiisexe.asp]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cptools/html/cpgrfaspnetiisregistratiext ctx = null; try { // Start impersonating. ctx = winId.Impersonate(); // Now impersonating. // Access resources using the identity of the authenticated user. } // Prevent exceptions from propagating. catch { } finally { // Revert impersonation. if (ctx != null) ctx.Undo(); } // Back to running under the default ASP.NET process identity.
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有