首先,我在頁面中寫於下列代碼來輸出3種不同的Credential。
Response.Write("Thread: " + Thread.CurrentPrincipal.Identity.Name+"<br/>");
Response.Write("Windws Identity: " + WindowsIdentity.GetCurrent().Name + "<br/>");
Response.Write("HttpContext: " + User.Identity.Name + "<br/>");
Thread:是輸出託管線程的identity。
Window Identity:輸出OS 的identity.一般是NT AUTHORITY\NETWORK SERVICE,因為.NET Framework寄宿在w3wp.exe(IIS>6.0)。
HttpContext:標識當前上下文Credential.
如果你用的是Windows驗證,則Thread,HttpContext為空白,但windows Identity為:NT AUTHORITY\NETWORK SERVICE.
所以此時,如果你將Impersonate='True',那麼windows Identity: NT AUTHORITY\IUSR
因為Impersonate類比的window賬戶.
當你用的是Forms驗證,則登入以後:
Thread: gary
Windws Identity: NT AUTHORITY\NETWORK SERVICE
HttpContext: gary
所以我們看到,Thread,HttpContext是在託管環境中設定的. 順便說一句,當你啟用windows 驗證,而你想設定Forms驗證一樣啟用了Authority.
<authorization>
<allow roles="Role1"/>
<deny users="*"/>
</authorization>
那麼總是會返回401 unauthority錯誤。因為不是Adminstrate 組