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 :/

0

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

3 Comments

that gives the server name, and i want the client name
oops sorry misread your question. You could use the win api as described here - 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.
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();
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> 

Comments

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.

1 Comment

Ok sorry I haven't got time to look into this in more detail but this might help you out - stackoverflow.com/questions/353601/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.