windows 身份驗證 在 windows 身份驗證中,iis 執行身份驗證,并將經過身份驗證的標記傳遞給 asp.net 工作進程。使用 windows 身份驗證的優點是它需要的編碼最少。在將請求傳遞給 asp.net 之前,您可能需要使用 windows 身份驗證來模擬 iis 進行驗證的 windows 用戶帳戶。
passport 身份驗證 passport 身份驗證是 microsoft 提供的集中式身份驗證服務,它為成員站點提供單一登錄和核心配置文件服務。通常,當您需要跨越多個域的單一登錄功能時,將使用 passport 身份驗證。
默認身份驗證 當 web 應用程序不需要任何安全功能時,將使用默認身份驗證;此安全提供程序需要匿名訪問。在所有的身份驗證提供程序中,默認身份驗證為應用程序提供了最高的性能。當您使用自己的自定義安全模塊時,也可以使用此身份驗證提供程序。
授權 授權是指驗證經身份驗證的用戶是否可以訪問請求資源的過程。
asp.net 提供以下授權提供程序: • fileauthorization • urlauthorization
fileauthorization fileauthorizationmodule 類進行文件授權,而且在使用 windows 身份驗證時處于活動狀態。fileauthorizationmodule 負責對 windows 訪問控制列表 (acl) 進行檢查,以確定用戶是否應該擁有訪問權限。 urlauthorization urlauthorizationmodule 類進行統一資源定位器 (url) 授權,它基于 uri 命名空間來控制授權。uri 命名空間可能與 ntfs 權限使用的物理文件夾和文件路徑存在很大的差異。
urlauthorizationmodule 實現肯定和否定的授權斷言;即,可以使用該模塊有選擇性地允許或拒絕訪問用戶、角色(如 manager、tester 和 administrator)和謂詞(如 get 和 post)的 uri 命名空間的任意部分。
基于角色的安全性 asp.net 中基于角色的安全性類似于 microsoft com+ 和 microsoft transaction server (mts) 所使用的基于角色的安全性,不過它們之間也存在很大的差異。asp.net 中基于角色的安全性不僅限于 windows 帳戶和組。例如,如果啟用 windows 身份驗證和模擬,用戶的標識就是 windows 標識 (user.identity.name = "domain/username")。可以檢查特定角色中成員的標識,并相應地限制其訪問權限。例如:
visual c# .net 代碼 if ( user.isinrole("builtin//administrators")) response.write("you are an admin"); else if (user.isinrole("builtin//users")) response.write("you are a user"); else response.write("invalid user");
如果使用的是表單身份驗證,則不會為經過身份驗證的用戶分配角色;您必須以編程方式執行此任務。若要為經過身份驗證的用戶分配角色,請使用身份驗證模塊(本例中為表單身份驗證模塊)的 onauthenticate 事件創建新的 genericprincipal 對象,并為其分配 httpcontext 的 user 屬性。以下代碼對此進行了說明:
visual c# .net 代碼 public void application_authenticaterequest(object s, eventargs e) { if (httpcontext.current.user != null) { if (httpcontext.current.user.identity.authenticationtype == "forms" ) { system.web.security.formsidentity id = httpcontext.current.user.identity; string[] myroles = new string[3]; myroles[0]= "managers"; myroles[1]= "testers"; myroles[2]= "developers"; httpcontext.current.user = new system.security.principal.genericprincipal(id,myroles); } } }
visual c# .net 代碼 if (user.isinrole("managers")) response.write("you are a manager"); else if (user.isinrole("testers")) response.write("you are a tester"); else if (user.isinrole("developers")) response.write("you are a developer");