用戶對象實現 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 |
用戶提供名稱 |