我試了很多代碼,它在我的本地服務器中成功運行。但我試圖把它放在遠程服務器,他們得到另一個字符串,如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();
什麼是認證的IIS設置? – danish
'System.Security.Principal.WindowsIdentity.GetCurrent()。Name'應該可以正常工作。請在IIS中提及您的身份驗證設置。 – Rohit416
如果您擁有正確的身份驗證設置並且用戶已登錄,則'HttpContext.Current.User'應該可以正常工作。如果您啓用了匿名身份驗證,則不會進行用戶身份驗證,而是使用應用程序池的帳戶。如果您使用任何其他類型的身份驗證,「HttpContext.Current.User」將返回登錄的用戶。如果您同時擁有匿名和其他選項,匿名將覆蓋其他選項 –