ASP.NET 中的 Windows 身分識別驗證

來源:互聯網
上載者:User

Windows身分識別驗證給開發人員提供了一個使用Windows平台及NTFS檔案系統內建安全特性的方式。它也利用了內建於IIS的安全特性。通過使用Windows身分識別驗證,可以使用很少,甚至不用代碼就可以構建一個高安全等級的asp.net應用程式。不過只有用戶端使用windows平台,並且在web伺服器上,或者web伺服器從屬的Windows域上有一個賬戶,Windows身分識別驗證才起作用。

有五種類型的Windows身分識別驗證:

1、基本驗證(Basic authentication);

2、摘要式驗證(Digest authentication);

3、整合Windows身分識別驗證(Integrated Windows authentication);

4、IIS的客戶認證映射驗證(IIS Client Certificate Mapping authentication);

5、活動目錄的客戶認證映射驗證(Active Directory Client Certificate Mapping authentication)。

如果不止一種身分識別驗證類型被啟用,比方說它們都被勾選中,那麼IIS會首先嘗試認證映射(Certificate Mapping),然後是整合Windows身分識別驗證。如果驗證失敗,那麼會接著嘗試摘要式驗證。最後,如果其他所有驗證都失敗,就會使用基本驗證。換句話說,系統會首先嘗試最安全的驗證選項,然後逐級向低安全層級的驗證選項過渡。

 

本教程闡釋在 ASP.NET 2.0 版中,IIS
整合Windows 身分識別驗證以及 ASP.NET Windows 身分識別驗證的工作機制。同時,闡釋 NTLM 和 Kerberos 身分識別驗證的工作機制。此外,本教程還闡釋 WindowsAuthenticationModule 類如何構造 WindowsPrincipal 和 WindowsIdentity 對象,然後將這些對象附加到當前的
ASP.NET Web 請求以表示經過身分識別驗證的使用者。

  概述

  身分識別驗證是一個驗證用戶端身份的過程,通常採用指定的第三方授權方式。用戶端可能是終端使用者、電腦、應用程式或服務。用戶端的標識稱為安全原則。為了使用伺服器應用程式進行驗證,用戶端提供某種形式的憑據來允許伺服器驗證用戶端的標識。確認了用戶端的標識後,應用程式可以授予執行操作和訪問資源的原則。

  如果應用程式使用 Active Directory 使用者儲存,則應該使用整合 Windows 身分識別驗證。對 ASP.NET 應用程式使用整合 Windows 身分識別驗證時,最好的方法是使用 ASP.NET 的 Windows 身分識別驗證提供者附帶的 Internet 資訊服務 (IIS) 驗證方法。使用該方法,將自動建立一個 WindowsPrincipal 對象(封裝一個 WindowsIdentity 對象)來表示經過身分識別驗證的使用者。您無需編寫任何身分識別驗證特定的代碼。

  ASP.NET 還支援使用 Windows 身分識別驗證的自訂解決方案(避開了 IIS 身分識別驗證)。例如,可以編寫一個根據 Active Directory 檢查使用者憑證的自訂 ISAPI 篩選器。使用該方法,必須手動建立一個 WindowsPrincipal 對象。

  ASP.NET 身分識別驗證

  IIS 向 ASP.NET 傳遞代表經過身分識別驗證的使用者或匿名使用者帳戶的令牌。該令牌在一個包含在 IPrincipal 對象中的 IIdentity 對象中維護,IPrincipal 對象進而附加到當前 Web 請求線程。可以通過 HttpContext.User 屬性訪問 IPrincipal 和 IIdentity 對象。這些對象和該屬性由身分識別驗證模組設定,這些模組作為 HTTP 模組實現並作為 ASP.NET 管道的一個標準部分進行調用, 3 所示。

  圖 3. ASP.NET 管道

  ASP.NET 管道模型包含一個 HttpApplication 對象、多個 HTTP 模組對象,以及一個 HTTP 處理常式對象及其相關的工廠對象。HttpRuntime 對象用於處理序列的開頭。在整個請求生命週期中,HttpContext 對象用於傳遞有關請求和響應的詳細資料。

  有關 ASP.NET 請求生命週期的詳細資料,請參閱"ASP.NET Life Cycle",網址是 http://msdn2.microsoft.com/library/ms227435(en-US,VS.80).aspx。

  身分識別驗證模組

  ASP.NET 2.0 在電腦層級的 Web.config 檔案中定義一組 HTTP 模組。其中包括大量身分識別驗證模組,如下所示:

以下是引用片段:
<httpModules>

  <add name="WindowsAuthentication"
    type="System.Web.Security.WindowsAuthenticationModule" />
  <add name="FormsAuthentication"
    type="System.Web.Security.FormsAuthenticationModule" />
  <add name="PassportAuthentication"
    type="System.Web.Security.PassportAuthenticationModule" />

</httpModules>  

  只載入一個身分識別驗證模組,這取決於該設定檔的 authentication 元素中指定了哪種身分識別驗證模式。該身分識別驗證模組建立一個 IPrincipal 對象並將它儲存在 HttpContext.User 屬性中。這是很關鍵的,因為其他授權模組使用該 IPrincipal 對象作出授權決定。

  當 IIS 中啟用匿名訪問且 authentication 元素的 mode 屬性設定為 none 時,有一個特殊模組將預設的匿名原則添加到 HttpContext.User 屬性中。因此,在進行身分識別驗證之後,HttpContext.User 絕不是一個Null 參考(在 Visual Basic 中為 Nothing)。

  WindowsAuthenticationModule

  如果 Web.config 檔案包含以下元素,則啟用 WindowsAuthenticationModule 類。

以下是引用片段:
<authentication mode="Windows" />

  WindowsAuthenticationModule 類負責建立 WindowsPrincipal 和 WindowsIdentity 對象來表示經過身分識別驗證的使用者,並且負責將這些對象附加到當前 Web 請求。

  對於 Windows 身分識別驗證,遵循以下步驟:

  •WindowsAuthenticationModule 使用從 IIS 傳遞到 ASP.NET 的 Windows 存取權杖建立一個 WindowsPrincipal 對象。該令牌封裝在 HttpContext 類的 WorkerRequest 屬性中。引發 AuthenticateRequest 事件時,WindowsAuthenticationModule 從 HttpContext 類檢索該令牌並建立 WindowsPrincipal 對象。HttpContext.User 用該 WindowsPrincipal
對象進行設定,它表示所有經過身分識別驗證的模組和 ASP.NET 頁的經過身分識別驗證的使用者的安全上下文。

  •WindowsAuthenticationModule 類使用 P/Invoke 調用 Win32 函數並獲得該使用者所屬的 Windows 組的列表。這些組用於填充 WindowsPrincipal 角色列表。

  •WindowsAuthenticationModule 類將 WindowsPrincipal Object Storage Service在 HttpContext.User 屬性中。隨後,授權模組用它對經過身分識別驗證的使用者授權。

  註:DefaultAuthenticationModule 類(也是 ASP.NET 管道的一部分)將 Thread.CurrentPrincipal 屬性設定為與 HttpContext.User 屬性相同的值。它在處理 AuthenticateRequest 事件之後進行此操作。

 

授權模組

WindowsAuthenticationModule 類完成其處理之後,如果未拒絕請求,則調用授權模組。授權模組也在電腦層級的 Web.config 檔案中的httpModules 元素中定義,如下所示:

<httpModules>  <add name="UrlAuthorization"     type="System.Web.Security.UrlAuthorizationModule" />  <add name="FileAuthorization"     type="System.Web.Security.FileAuthorizationModule" />  <add name="AnonymousIdentification"     type="System.Web.Security.AnonymousIdentificationModule" /></httpModules>

UrlAuthorizationModule

調用 UrlAuthorizationModule 類時,它在電腦層級或應用程式特定的 Web.config 檔案中尋找authorization 元素。如果存在該元素,則UrlAuthorizationModule 類從HttpContext.User 屬性檢索
IPrincipal 對象,然後使用指定的動詞(GET、POST 等)來確定是否授權該使用者訪問請求的資源。

FileAuthorizationModule

接下來,調用 FileAuthorizationModule 類。它檢查 HttpContext.User.Identity 屬性中的IIdentity 對象是否是WindowsIdentity 類的一個執行個體。如果
IIdentity 對象不是 WindowsIdentity 類的一個執行個體,則
FileAuthorizationModule
類停止處理。

如果存在 WindowsIdentity 類的一個執行個體,則 FileAuthorizationModule 類調用AccessCheck Win32 函數(通過 P/Invoke)來確定是否授權經過身分識別驗證的用戶端訪問請求的檔案。如果該檔案的安全性描述元的隨機存取控制清單 (DACL) 中至少包含一個Read 存取控制項目 (ACE),則允許該請求繼續。否則,FileAuthorizationModule
類調用HttpApplication.CompleteRequest 方法並將狀態代碼 401 返回到用戶端。

安全上下文

.NET Framework 使用以下兩個介面封裝 Windows 令牌和登入工作階段:

System.Security.Principal.IPrincipal

System.Security.Principal.IIdentity(它公開為 IPrincipal 介面中的一個屬性。)

HttpContext.User

在 ASP.NET 中,用 WindowsPrincipalWindowsIdentity 類表示使用 Windows 身分識別驗證進行身分識別驗證的使用者的安全上下文。使用 Windows 身分識別驗證的 ASP.NET 應用程式可以通過HttpContext.User 屬性訪問WindowsPrincipal 類。

要檢索啟動當前請求的 Windows 經過身分識別驗證的使用者的安全上下文,使用以下代碼:

using System.Security.Principal;...// Obtain the authenticated user's IdentityWindowsPrincipal winPrincipal = (WindowsPrincipal)HttpContext.Current.User;

WindowsIdentity.GetCurrent

WindowsIdentity.GetCurrent 方法可用於獲得當前啟動並執行 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 應用程式中的每個線程公開一個 CurrentPrincipal 對象,該對象儲存經過身分識別驗證的初始使用者的安全上下文。該安全上下文可用於基於角色的授權。

要檢索線程的當前原則,使用以下代碼:

using System.Security.Principal;...// Obtain the authenticated user's identityWindowsPrincipal winPrincipal = (WindowsPrincipal) Thread.CurrentPrincipal();

表 1 顯示從各種識別屬性獲得的結果標識,當您的應用程式使用 Windows 身分識別驗證且 IIS 配置為使用整合 Windows 身分識別驗證時,可以從 ASP.NET 應用程式使用這些識別屬性。

表 1:線程公開的 CurrentPrincipal Object
Web.config 設定 變數位置 結果標識

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

HttpContext
WindowsIdentity
線程

Domain\UserName
Domain\UserName
Domain\UserName

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

HttpContext
WindowsIdentity
線程

Domain\UserName
NT AUTHORITY\NETWORK SERVICE
Domain\UserName

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

HttpContext
WindowsIdentity
線程

使用者提供的名稱
Domain\UserName
使用者提供的名稱

<identity impersonate="false"/>
<authentication mode="Forms" />

HttpContext
WindowsIdentity
線程

http://dev.yesky.com/msdn/39/7595039.shtml

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.