我尝试了很多代码,在我的本地服务器上可以成功运行。但是当我将其放到远程服务器上时,会得到一些其他字符串,例如IIS APPPOOL,“servername”等。我在IIS管理器中更改了一些设置,但失败了。
如何在远程服务器上获取用户名?
我尝试了一些代码:
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();
System.Security.Principal.WindowsIdentity.GetCurrent().Name
应该可以正常工作。请在 IIS 中提及您的身份验证设置。 - Rohit416HttpContext.Current.User
应该可以工作。如果启用了匿名身份验证,则不会进行用户身份验证,而是使用应用程序池的帐户。如果您使用任何其他类型的身份验证,则HttpContext.Current.User
将返回已登录的用户。如果您同时具有匿名和其他选项,则匿名将覆盖其他选择。 - Panagiotis Kanavos