用户对象实现 IPrincipal 接口,并且表示用户的安全性上下文,代码基于此用户运行。用户对象包括用户的身份(作为包含的 IIdentity 对象)以及用户所属的任何角色。
ASP.NET 提供了以下用户和身份对象实现:
• | WindowsPrincipal 和 WindowsIdentity 对象代表用 Windows 身份验证进行验证的用户。利用这些对象,角色列表自动从 Windows 组集中获取,Windows 用户属于该组。 |
• | GenericPrincipal 和 GenericIdentity 对象代表用 Forms 身份验证或其他自定义身份验证机制进行验证的用户。利用这些对象,角色列表通常从数据库中以自定义方式获取。 |
• | FormsIdentity 和 PassportIdentity 对象代表分别用 Forms 和 Passport 身份验证机制进行验证的用户。 |
下面的表格说明了对于一系列的身份验证设置,从每个变量获得的结果身份保持一个 IPrincipal 和/或 IIdentity 对象。在表格中使用了一下缩写:
• | HttpContext = HttpContext.Current.User,其中 返回一个 IPrincipal 对象,它包含当前 Web 请求的安全性信息。这是经身份验证的 Web 客户端。 |
• | WindowsIdentity = WindowsIdentity.GetCurrent(),返回当前执行 Win32 线程的安全性上下文。 |
• | Thread = Thread.CurrentPrincipal,返回当前执行 .NET 线程的用户,该线程在 Win32 线程上运行。 |
IIS 匿名身份验证
Web.config 设置 | 变量位置 | 结果身份 |
<identity impersonate="true"/><authentication mode="Windows" /> |
HttpContext |
- |
<identity impersonate="false"/><authentication mode="Windows" /> |
HttpContext |
- |
<identity impersonate="true"/><authentication mode="Forms" /> |
HttpContext |
Name provided by user |
<identity impersonate="false"/><authentication mode="Forms" /> |
HttpContext |
Name provided by user |
IIS 基本身份验证
Web.config 设置 | 变量位置 | 结果身份 |
<identity impersonate="true"/><authentication mode="Windows" /> |
HttpContext |
域/用户名 |
<identity impersonate="false"/><authentication mode="Windows" /> |
HttpContext |
域/用户名 |
<identity impersonate="true"/><authentication mode="Forms" /> |
HttpContext |
用户提供名称 |
<identity impersonate="false"/><authentication mode="Forms" /> |
HttpContext |
用户提供名称 |
IIS 简要身份验证
Web.config: | 变量位置 | 结果身份 |
<identity impersonate="true"/><authentication mode="Windows" /> |
HttpContext |
域/用户名 |
<identity impersonate="false"/><authentication mode="Windows" /> |
HttpContext |
域/用户名 |
<identity impersonate="true"/><authentication mode="Forms" /> |
HttpContext |
用户提供名称 |
<identity impersonate="false"/><authentication mode="Forms" /> |
HttpContext |
用户提供名称 |
IIS 集成 Windows
Web.config 设置 | 变量位置 | 结果身份 |
<identity impersonate="true"/><authentication mode="Windows" /> |
HttpContext |
域/用户名 |
<identity impersonate="false"/><authentication mode="Windows" /> |
HttpContext |
域/用户名 |
<identity impersonate="true"/><authentication mode="Forms" /> |
HttpContext |
用户提供名称 |
<identity impersonate="false"/><authentication mode="Forms" /> |
HttpContext.WindowsIdentity |
用户提供名称 |