熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

ASP.NET 2.0 中的Windows身份驗證

2022-06-13   來源: .NET編程 

  本教程闡釋在 ASPNET 版中IIS 集成 Windows 身份驗證以及 ASPNET 的Windows 身份驗證的工作機制同時闡釋 NTLM 和 Kerberos 身份驗證的工作機制此外本教程還闡釋 WindowsAuthenticationModule 類如何構造 WindowsPrincipal 和 WindowsIdentity 對象然後將這些對象附加到當前的 ASPNET Web 請求以表示經過身份驗證的用戶

  概述

  身份驗證是一個驗證客戶端身份的過程通常采用指定的第三方授權方式客戶端可能是最終用戶計算機應用程序或服務客戶端的標識稱為安全原則為了使用服務器應用程序進行驗證客戶端提供某種形式的憑據來允許服務器驗證客戶端的標識確認了客戶端的標識後應用程序可以授予執行操作和訪問資源的原則

  如果應用程序使用 Active Directory 用戶存儲則應該使用集成 Windows 身份驗證對 ASPNET 應用程序使用集成 Windows 身份驗證時最好的方法是使用 ASPNET 的 Windows 身份驗證提供程序附帶的 Internet 信息服務 (IIS) 身份驗證方法使用該方法將自動創建一個 WindowsPrincipal 對象(封裝一個 WindowsIdentity 對象)來表示經過身份驗證的用戶您無需編寫任何身份驗證特定的代碼

  ASPNET 還支持使用 Windows 身份驗證的自定義解決方案(避開了 IIS 身份驗證)例如可以編寫一個根據 Active Directory 檢查用戶憑據的自定義 ISAPI 篩選器使用該方法必須手動創建一個 WindowsPrincipal 對象

  ASPNET 身份驗證

  IIS 向 ASPNET 傳遞代表經過身份驗證的用戶或匿名用戶帳戶的令牌該令牌在一個包含在 IPrincipal 對象中的 IIdentity 對象中維護IPrincipal 對象進而附加到當前 Web 請求線程可以通過 HttpContextUser 屬性訪問 IPrincipal 和 IIdentity 對象這些對象和該屬性由身份驗證模塊設置這些模塊作為 HTTP 模塊實現並作為 ASPNET 管道的一個標准部分進行調用如圖 所示

  ASP.NET 身份驗證

  圖 ASPNET 管道

  ASPNET 管道模型包含一個 HttpApplication 對象多個 HTTP 模塊對象以及一個 HTTP 處理程序對象及其相關的工廠對象HttpRuntime 對象用於處理序列的開頭在整個請求生命周期中HttpContext 對象用於傳遞有關請求和響應的詳細信息

  有關 ASPNET 請求生命周期的詳細信息請參閱ASPNET Life Cycle網址是 (enUSVSaspx

  身份驗證模塊

  ASPNET 在計算機級別的 nfig 文件中定義一組 HTTP 模塊其中包括大量身份驗證模塊如下所示

  

  <httpModules>
  <add name=WindowsAuthentication
    type=SystemWebSecurityWindowsAuthenticationModule />
  <add name=FormsAuthentication
    type=SystemWebSecurityFormsAuthenticationModule />
  <add name=PassportAuthentication
    type=SystemWebSecurityPassportAuthenticationModule />

</httpModules>

  只加載一個身份驗證模塊這取決於該配置文件的 authentication 元素中指定了哪種身份驗證模式該身份驗證模塊創建一個 IPrincipal 對象並將它存儲在 HttpContextUser 屬性中這是很關鍵的因為其他授權模塊使用該 IPrincipal 對象作出授權決定

  當 IIS 中啟用匿名訪問且 authentication 元素的 mode 屬性設置為 none 時有一個特殊模塊將默認的匿名原則添加到 HttpContextUser 屬性中因此在進行身份驗證之後HttpContextUser 絕不是一個空引用(在 Visual Basic 中為 Nothing)

  WindowsAuthenticationModule

  如果 nfig 文件包含以下元素則激活 WindowsAuthenticationModule 類

  

  <authentication mode=Windows />

  WindowsAuthenticationModule 類負責創建 WindowsPrincipal 和 WindowsIdentity 對象來表示經過身份驗證的用戶並且負責將這些對象附加到當前 Web 請求

  對於 Windows 身份驗證遵循以下步驟

  WindowsAuthenticationModule 使用從 IIS 傳遞到 ASPNET 的 Windows 訪問令牌創建一個 WindowsPrincipal 對象該令牌包裝在 HttpContext 類的 WorkerRequest 屬性中引發 AuthenticateRequest 事件時WindowsAuthenticationModule 從 HttpContext 類檢索該令牌並創建 WindowsPrincipal 對象HttpContextUser 用該 WindowsPrincipal 對象進行設置它表示所有經過身份驗證的模塊和 ASPNET 頁的經過身份驗證的用戶的安全上下文

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

  WindowsAuthenticationModule 類將 WindowsPrincipal 對象存儲在 HttpContextUser 屬性中隨後授權模塊用它對經過身份驗證的用戶授權

  注DefaultAuthenticationModule 類(也是 ASPNET 管道的一部分)將 ThreadCurrentPrincipal 屬性設置為與 HttpContextUser 屬性相同的值它在處理 AuthenticateRequest 事件之後進行此操作


From:http://tw.wingwit.com/Article/program/net/201311/13618.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.