【发布时间】:2015-05-06 07:22:51
【问题描述】:
我尝试了很多代码,它在我的本地服务器上成功运行。但我试着把远程服务器放在他们得到另一个字符串,如 IIS APPPOOL、“服务器名”...。我在 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();
【问题讨论】:
-
IIS 上的身份验证设置是什么?
-
System.Security.Principal.WindowsIdentity.GetCurrent().Name应该可以正常工作。请务必提及您在 IIS 中的身份验证设置。 -
HttpContext.Current.User应该可以工作如果您具有正确的身份验证设置并且用户已登录。如果您启用了匿名身份验证,则不会进行用户身份验证并且应用程序池的而是使用帐户。如果您使用任何其他类型的身份验证,HttpContext.Current.User将返回登录用户。如果您同时拥有匿名和其他选项,则匿名会覆盖其他选项 -
我都试过了,它在我的本地服务器上工作,但在远程服务器上不工作。另外,我不知道在 IIS 管理器中该做什么。
-
匿名身份验证也必须禁用,否则优先 - 网络浏览器总是首先尝试匿名连接
标签: c# asp.net iis windows-authentication