2012-12-27 28 views
0

我有以下代碼:無法獲得域用戶

protected void Page_Load(object sender, EventArgs e) 
    { 
     string infors = ""; 
     System.Security.Principal.WindowsIdentity usr = System.Security.Principal.WindowsIdentity.GetCurrent(); 
     infors += "Authentication Type: " + usr.AuthenticationType + "<br>"; 
     infors += "Name: " + usr.Name + "<br>"; 
     infors += "IsAnonymous: " + usr.IsAnonymous + "<br>"; 
     infors += "IsAuthenticated: " + usr.IsAuthenticated + "<br>"; 
     infors += "IsGuest: " + usr.IsGuest + "<br>"; 
     infors += "IsSystem: " + usr.IsSystem + "<br>"; 
     infors += "Token: " + usr.Token.ToString() + "<br>"; 

     Label1.Text = infors; 
    } 

然而,一直給我上Name部分錯誤信息

 
Authentication Type: NTLM 
Name: SECTOR_A\Administrator 
IsAnonymous: False 
IsAuthenticated: True 
IsGuest: False 
IsSystem: False 
Token: 564 

我的IIS設置爲:

 
Anonymous Authentication - Disabled 
ASP.NET Impersonation - Enabled 
Basic Authentication - Enabled 
Windows Authentication - Enabled 

我甚至改變IE設置,設置爲Prompt for user name and password,如下圖所示

enter image description here

======然而======

雖然我進入,在彈出的登錄信息的域用戶名和密碼,當我打開Web應用程序,它沒有給我正確的Name部分,應該是SECTOR_HQ\Jack

所以我想問一下在我的代碼和配置中是否有什麼缺失。

======新增的Web.config ======

<configuration> 

    <system.web> 
     <compilation debug="true" targetFramework="4.0" /> 

    </system.web> 

</configuration> 

======應用程序池的信息======

 
.NET Framework Version: v4.0 
Managed Pipeline Mode: Classic 
Identity: ApplicationPoolIdentity 
+1

你能告訴在web.config中的身份驗證部分? – 2GDev

+0

@ 2GDev,謝謝,我爲您的信息添加了問題。 – Jack

+0

爲什麼不使用'this.User'屬性? –

回答

0

嘗試添加驗證部件在你的配置:

<system.web> 
    ... 
    <authentication mode="Windows"/> 
    <identity impersonate="true"/> 
    ... 
</system.web> 

個在這裏你可以找到更多的細節:

http://msdn.microsoft.com/en-us/library/ff647405.aspx

+0

好,現在我能夠看到域用戶。但是,有一個問題是,當我使用域用戶名登錄到PC時,它仍會提示我輸入用戶名和密碼。如果用戶使用域用戶名和密碼登錄到PC,是否有跳過它的地方? – Jack

+0

順便提一句,您提供的鏈接是針對ASP.NET 2的,而不是我使用.NET 4的鏈接。它是否仍然能夠申請.NET 4? – Jack

+0

@Jack可能在您的IE選項中設置了「使用當前用戶名和密碼自動登錄」。 我無法找到任何關於asp.net 2.0和4.0認證設置的區別。我會檢查是否有任何。 – 2GDev