2013-08-16 44 views
4

在Visual Studio中,我使用他們提供的模板創建了一個全新的ASP.NET Web窗體應用程序。如何在新的Web應用程序的Global.asax中實現User.Identity.Name

我還沒有做什麼到VS自動生成除了添加以下的global.asax.cs

string myself = System.Security.Principal.WindowsIdentity.GetCurrent().Name; 
string me = User.Identity.Name; 

字符串自己保存我當前的Windows成功登錄ID代碼。但字符串我是空的,因爲當我調試時,用戶爲空。但是,Visual Studio在調試之前顯示沒有錯誤。

如何使用User.Identity.Name獲取正確的字符串值?

回答

8

輸入代碼:

void Session_Start(object sender, EventArgs e) 
{ 
    string myself = System.Security.Principal.WindowsIdentity.GetCurrent().Name; 
    string me = User.Identity.Name; 
    Response.Write("myself:" + myself + "<br>me:" + me); 
} 
+0

是的,這有訣竅。我之前有了Application_Start()中的代碼。 – Krondorian

+0

...和WindowsIdentity和User.Identity之間的一個很好的對比。 – Rap

0

你使用了什麼認證?我不記得副手,但匿名身份驗證可能在User中呈現null或null類似的對象。在在session_start

+0

我想它使用Windows身份驗證。我想我需要修改web.config 爲了實現這一點,但我試過的東西沒有奏效。 – Krondorian

相關問題