问题
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