Windows 身份验证提供程序

WindowsAuthenticationModule 提供程序依赖 Microsoft Internet 信息服务 (IIS) 来提供经过身份验证的用户,它使用 IIS 支持的任何机制。如果需要用最少的 ASP.NET 代码实现站点安全机制,则应当使用该提供程序配置。提供程序模块构造 WindowsIdentity 对象。默认实现构造 WindowsPrincipal 对象并将其附加到应用程序上下文。WindowsPrincipal 对象将身份映射为 Windows 组。

如果使用 IIS 身份验证,则提供程序模块使用从 IIS 传入的已验证身份。IIS 使用基本、简要、集成 Windows 身份验证或这些验证的组合来验证身份。可以使用模拟和 NTFS ACL 权限来限制或允许对受保护资源的访问。

使用 WindowsAuthenticationModule 提供程序的一个重要原因就是实现模拟方案,该方案可以使用 IIS 在将请求传递给 ASP.NET 应用程序之前已经执行的任何身份验证方法。要做到这一点,请将身份验证模式设置为 Windows,并确认 impersonate 元素设置为 true,如下面的示例所示:

<authentication mode="Windows"/>
<identity impersonate="true"/>

请注意,配置 ASP.NET 应用程序对“IIS 目录安全性”设置无效。系统是完全独立的并按顺序应用。除了为 ASP.NET 应用程序选择身份验证模式外,正确地配置 IIS 身份验证也很重要。

下一步必须设置 NTFS ACL 以只允许对正确的标识进行访问。如果只需要在处理请求期间短时间启用模拟,则可以通过使用模拟上下文和 WindowsIdentity.Impersonate 来实现。

首先将 impersonate 元素设置为 false,然后使用 WindowsIdentity.Impersonate 方法设置上下文,如下面的示例所示。

[Visual Basic]
Dim context As WindowsImpersonationContext = _
WindowsIdentity.Impersonate(impersonateToken)
' Perform some action.
context.Undo()
[C#]
WindowsImpersonationContext context = WindowsIdentity.Impersonate(impersonateToken);
// Perform some action.
context.Undo();

注意,可以使用 context.Undo 来恢复身份。

如前所述,可以使用 WindowsAuthentication_OnAuthenticate 事件处理程序从 WindowsIdentity 对象创建 WindowsPrincipalGenericPrincipal 对象,来实现自定义 Windows 授权方案。然后可以使用这些新对象之一来实现自己的自定义身份验证方案。WindowsPrincipal 对象将身份映射为 Windows 组。默认实现构造 WindowsPrincipal 对象并将其附加到应用程序上下文。