3

how can I get the client's computer name in a web application. The user in a network.

Regards

// Already tryed this option

string IP = System.Web.HttpContext.Current.Request.UserHostAddress;
string compName = DetermineCompName(IP);

System.Net.IPHostEntry teste = System.Net.Dns.GetHostEntry(IP);
ssresult = IP + "   -   " + teste.HostName;

// TODO: Write implementation for action

private static string DetermineCompName(string IP)
{
   IPAddress myIP = IPAddress.Parse(IP);
   IPHostEntry GetIPHost = Dns.GetHostEntry(myIP);
   string[] compName = GetIPHost.HostName.ToString().Split('.');
   return compName[0];
}

All of that, gives me only the IP :/

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Filipe
  • 31
  • 1
  • 2

4 Answers4

3

You can't do this in a way that is guaranteed to work.

The closest you will be able to get is to go down the route of doing a reverse dns lookup using System.Net.Dns.GetHostEntry which is what you have already tried.

The problem is that your machine has no way of knowing the hostname of a remote web client via its IP address alone (unless it is on the same subnet, in which case you may be able to retrieve it).

You have to fall back on your DNS infrastructure being able to map the IP back into a hostname [this is what nslookup does when you type in a hostname], and plenty of places just won't bother setting up reverse IP records.

Plus, often if they do, they won't match the hostname. It is quite common to see a reverse lookup for "1.2.3.4" come back as something line "machine-1.2.3.4", instead of the actual hostname.

This problem is exacerbated further if the clients are behind any sort of Network Address Translation so that many client computers have a single IP from an external perspective. This is probably not the case for you since you state "The user in a network".

As an aside, if you go to the server and type in, at a command prompt,

nslookup <some ip>

(where is an example of one of these client machines), do you get a hostname back?

If you do, then System.Net.Dns.GetHostEntry should be able to as well, if not then it probably can't for the reasons mentioned above.

Rob Levine
  • 40,328
  • 13
  • 85
  • 111
0

you can get the windows machine name with - System.Environment.MachineName

  • that gives the server name, and i want the client name – Filipe May 17 '10 at 16:11
  • oops sorry misread your question. You could use the win api as described here - http://www.codeproject.com/KB/IP/ListNetworkComputers.aspx I know not a great option as it would require you to cache a list of all machines in the network. – ClearCarbon May 17 '10 at 16:17
  • Just had a further look and you could run nslookup with System.Diagnostics.Process process = new System.Diagnostics.Process(); System.Diagnostics.ProcessStartInfo sInfo = new System.Diagnostics.ProcessStartInfo("nslookup.exe"); result = process.StandardOutput.ReadToEnd(); – ClearCarbon May 17 '10 at 16:21
0

Yeah would require you to keep requesting and caching the computers terribly inefficient, was a bad idea.

I would go with running nslookup in the background I haven't tested this code and neither is it handling errors for failures, but basically, you can do:

System.Diagnostics.Process process = new System.Diagnostics.Process(); 
System.Diagnostics.ProcessStartInfo sInfo = new System.Diagnostics.ProcessStartInfo("nslookup.exe", "192.168.1.100");
string result = process.StandardOutput.ReadToEnd(); 

Then just parse the result value.

sshow
  • 8,820
  • 4
  • 51
  • 82
  • Ok sorry I haven't got time to look into this in more detail but this might help you out - http://stackoverflow.com/questions/353601/capturing-nslookup-shell-output-with-c – ClearCarbon May 18 '10 at 08:38
0

Assuming your clients are Windows based running IE you could use this client side code to get the names and pass them back to the server:

<script type="text/javascript"> 
        function create() 
        { 
           var net = new ActiveXObject("wscript.network"); 
           document.write(net.ComputerName); 
        } 
</script> 
John Wiese
  • 1,601
  • 1
  • 12
  • 9