2010-02-02 85 views
8

Page.User.Identity和Request.LogonUserIdentity之間有什麼區別(幕後)?不是類型,名稱等方面的差異,而是它們在後臺如何實現的區別(即一個調用windows xxx api,另一個調用asp.net xxx api ...)。Page.User.Identity與Request.LogonUserIdentity之間的差異

+0

您似乎已經回答了您自己的問題。你有什麼特別的想法嗎? LogonUserIdentity返回當前登錄用戶的WindowsIdentity對象。 – sarvesh 2010-02-02 23:18:02

回答

18

這取決於您使用什麼機制來驗證用戶身份,以及您對模擬的設置。

例如,根據VS開發服務器,使用Forms身份驗證,標準的SQL成員資格提供以下代碼:

// m_LoggedIn is a Literal control on the page: 
m_LoggedIn.Text = string.Format("<br />Page.User.Identity: {0} " + 
           "<br />Request.LogonUserIdentity: {1}", 
           Page.User.Identity.Name, 
           Request.LogonUserIdentity.Name); 

我得到以下輸出:

Page.User。身份:zhaph

Request.LogonUserIdentity:[計算機] \奔

第一行(Page.User.Identity)是我登錄到網站的表單身份驗證帳戶,第二行是請求正在運行的Windows標識 - 因爲我沒有啓用模擬,這是我的Windows登錄,因爲這是運行Web服務器的帳戶。

在API的方面,HttpRequest.LogonUserIdentity被調用到的WindowsIdentity類,它將始終代表Windows用戶帳戶,而Page.User正在創造一個實現IPrinciple一個對象,它允許您使用數來表示用戶不同的後備存儲 - 例如由MembershipProvider提供的SQL數據庫結構。