Getting computers name using c#

﹥>﹥吖頭↗ 提交于 2020-01-03 02:52:21

问题


I am using this code in cs file into an asp.net page for getting ip address from the logged user:

cmd.Parameters.AddWithValue("@ip",System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]);

I would like to have also his computer name, as well. How do I do that?


回答1:


This is not necessarily possible.

You can attempt to reverse lookup the name from the ip address, using something like

private string[] GetHostnamesForIpAddress(string ipAddress)
{
   var hostIp= IPAddress.Parse(ipAddress);
   IPHostEntry hostInfo = Dns.GetHostByAddress(hostIp);

   return hostInfo.Aliases;
}

On a local network (where your client is local to you, e.g. on a corporate network), this may well be ok, as long as all the clients have reverse ip mappings in DNS.

Over the internet, it is far less likely to work for most clients. You only have the IP address to go on, and generally these won't have reverse DNS mappings set up. In fact, an awful lot of machines out there will be behind proxies and NAT gateways and only have private, non-routable ip addresses, for which you can't possibly do a reverse lookup.



来源:https://stackoverflow.com/questions/3227550/getting-computers-name-using-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!