如何在 c# 和 asp.net 中获取客户端主机名、本地计算机名和用户名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11417640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How I can obtain client hostname, local computer name and user name in c# and asp.net
提问by laspalmos
I have the IP address, but for some reason I can get the name resolve correctly to show local computer name. I try few things and all of them is showing the server hostname?
我有 IP 地址,但由于某种原因,我可以正确解析名称以显示本地计算机名称。我尝试了几件事,但所有这些都显示了服务器主机名?
ipadd = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
IPAddress myIP = IPAddress.Parse(ipadd);
IPHostEntry GetIPHost = Dns.GetHostEntry(myIP);
//userhostname = System.Environment.MachineName;
//userhostname = Dns.GetHostName(); //Resolve ServerHostName not computer
//userhostname = HttpContext.Current.Request.UserHostName;
//userhostname = HttpContext.Current.Request.UserAgent;
userhostname = GetIPHost.HostName;
For user name I am tring to use
对于用户名,我想使用
nametext = WindowsIdentity.GetCurrent().Name;
//Shows server name when deployed on server
//when debuging and running localy shows correctly current user logged in
//nametext = HttpContext.Current.Request.ServerVariables["LOGON_USER"];
// not returning anything
I switched authentication and no results
我切换了身份验证但没有结果
<authentication mode="Forms">
<authentication mode="Windows">
采纳答案by jrummell
Use HttpRequest.UserHostAddressand HttpRequest.UserHostNamefor client IP and machine name.
将HttpRequest.UserHostAddress和HttpRequest.UserHostName用于客户端 IP 和机器名称。
Assuming you have authentication configured correctly, you can get the client user from IIdentity.Name.
假设您已正确配置身份验证,您可以从IIdentity.Name.
In the context of a Page, you can use Request.UserHostAddress, Request.UserHostNameand User.Identity.Name.
在 a 的上下文中Page,您可以使用Request.UserHostAddress,Request.UserHostName和User.Identity.Name。
回答by yonel
I use this to show the client locale machine on my intranet (using System.Net) :
我用它来显示我的 Intranet 上的客户端语言环境机器(使用 System.Net):
Dns.GetHostEntry(Request.UserHostAddress).HostName.ToString()

