views:

40

answers:

1

Hello,

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

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

I would like to have also his computer name, as well?!

+2  A: 

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.

Rob Levine
Dear Mr. Levine,Thank you for this good explanation :D ... all the best !
gaponte69
@gaponte69 - my pleasure :)
Rob Levine