2016-08-03 40 views
0

我做了一個web應用程序,它顯示右上角的Windows用戶名。 如果我打開localhost:5903/index.aspx上的webapp,我會得到正確的Windows用戶。 這樣的 -


enter image description hereIIS沒有拿起Windows用戶

但是,如果我打開localhost/db/index.aspx我得到IIS應用程序池的用戶 -


enter image description here

這裏DB是有我的應用程序的目錄。

我不知道爲什麼會發生這種情況。我希望localhost也顯示正確的用戶名。

我的IIS樹,這裏DB(1)是我的webapp -

enter image description here

我的C#代碼,其拾取Windows身份驗證 -

<li><% string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;%> 
<a><%= userName %></a> 
</li> 

回答

2

您需要設置在IIS網站僅限Windows身份驗證並關閉匿名。

進入IIS for your site然後在Features View下。

然後在IIS類別>身份驗證>將Windows身份驗證設置爲已啓用,將所有其他(匿名....其他)設置爲已禁用。

而不是...

System.Security.Principal.WindowsIdentity.GetCurrent().Name; 

使用

HttpContext.Current.User.Identity.Name; 
+0

Windows身份驗證已經啓用。我禁用匿名。不用找了。仍然得到IIS AppPool –

+0

更新了上面的答案..使用HttpContext.Current.User.Identity.Name; –

+0

它的工作!你能告訴我有什麼區別,爲什麼它不與本地主機一起工作? –