我尝试了很多代码,它在我的本地服务器上成功运行。但我尝试将远程服务器放到另一个字符串,如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();
答案 0 :(得分: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
答案 1 :(得分:3)
HttpContext.Current.User
应该没问题,但 Windows身份验证已启用的事实还不够:还要确保禁用匿名身份验证。