如何在web api2中从请求中获取远程用户名

时间:2018-03-22 07:42:05

标签: c# asp.net-mvc asp.net-web-api2

我需要在webApi 2.0中识别远程用户名及其域名,但无法这样做。 我试过用一些东西

ViewBag.userName1 = WindowsIdentity.GetCurrent()。Name;

ViewBag.userName2 = System.Web.HttpContext.Current.User.Identity.Name.ToLower();

 // will return the host name making the request
 ViewBag.remoteHost = Request.ServerVariables["REMOTE_HOST"];

// will return the computer name
 ViewBag.logonUser = Request.ServerVariables["LOGON_USER"]; 
 ViewBag.remoteUser = Request.ServerVariables["REMOTE_USER"];

 // will return the computer name
 ViewBag.serverName = Request.ServerVariables["SERVER_NAME"];

 System.Net.IPAddress[] strClientIPAddress = 
 System.Net.Dns.GetHostAddresses(Environment.MachineName);
            ViewBag.strClientMachineName = 
 Environment.MachineName.ToString().Trim();
            ViewBag.strClientUserName = Environment.UserName.ToString().Trim();
            ViewBag.trClientDomainName = 
 Environment.UserDomainName.ToString().Trim();
            ViewBag.strClientOSVersion = 
 Environment.OSVersion.ToString().Trim();

,输出

NT AUTHORITY\IUSR 
null
10.91.145.111 
null
null
10.91.145.88 
53323-TZD-SITA 
IUSR 
NT AUTHORITY 
Microsoft Windows NT 6.3.9600.0

任何人都可以帮我获取远程用户名和域名吗?

1 个答案:

答案 0 :(得分:0)

我在IIS中启用了窗口身份验证后解决了我的问题。