2015-05-06 33 views
3

我試了很多代碼,它在我的本地服務器中成功運行。但我試圖把它放在遠程服務器,他們得到另一個字符串,如IIS APPPOOL,「服務器名稱」....我更改爲IIS管理器中的一些設置,但我失敗了。 我怎樣才能在遠程服務器獲取用戶名如何在asp.net中獲取當前的windows用戶?

我試着像一些代碼:

System.Security.Principal.WindowsPrincipal= System.Threading.Thread.CurrentPrincipal as System.Security.Principal.WindowsPrincipal; 

    string strName = p.Identity.Name; 

string strName = HttpContext.Current.User.Identity.Name.ToString(); 

string Name = Environment.GetEnvironmentVariable("USERNAME"); 

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

WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent(); 
    string usernamex = currentIdentity.Name.ToString(); 

HttpContext.Current.User.Identity.Name.ToString(); 
+1

什麼是認證的IIS設置? – danish

+1

'System.Security.Principal.WindowsIdentity.GetCurrent()。Name'應該可以正常工作。請在IIS中提及您的身份驗證設置。 – Rohit416

+1

如果您擁有正確的身份驗證設置並且用戶已登錄,則'HttpContext.Current.User'應該可以正常工作。如果您啓用了匿名身份驗證,則不會進行用戶身份驗證,而是使用應用程序池的帳戶。如果您使用任何其他類型的身份驗證,「HttpContext.Current.User」將返回登錄的用戶。如果您同時擁有匿名和其他選項,匿名將覆蓋其他選項 –

回答

3

HttpContext.Current.User應該沒問題,但Windows身份驗證啓用的事實還不夠:還確保匿名身份驗證是禁用的。

4

假設您正在使用IIS 7.5,請執行以下操作。其他版本會相似。所有上述獲取用戶名的方式都應該可以工作。

1) Double click on Authentication (This can be done at server level and your web site level so check both!) 
2) Right click on Anonymous authentication and disable it and likewise enable Windows authentication 
3) Click Basic settings. Check which application pool your website is using. Ensure it is using pass-through authentication for an "Application User" by clicking "Connect As" i.e. A user using a browser will be the person requesting authentication. 
4) Click "Application Pools". Click the Application pool from (3). Click "Advanced Settings". Check the Identity is set to ApplicationPoolIdentity 

Double click on Authentication

Right click on Anonymous authentication and disable it and likewise enable Windows authentication

Check which application pool your website is using Check the Application pool settings

+0

謝謝很多沙林,但我不明白爲什麼我正在設置匿名身份驗證禁用。並且它仍然不起作用。我無法獲得Windows用戶的用戶名 –

+0

您是否在服務器和網站上禁用了匿名身份驗證?我上面的圖片只顯示點擊服務器,但你一定要點擊網站並從那裏禁用它。服務器是可選的,因爲您可能希望您託管的其他網站的工作方式不同。 – sarin

+0

當匿名身份驗證是禁用網站想要得到但我想輸入密碼進行身份驗證。我怎樣才能設置服務器和網站? –

相關問題