44

I am building an intranet site that will display different lists based on the computer name because different computers are in different areas, is there a way (within a controller or model) to determine the client's computer name?

I have tried system.environment.machinename but that only returns the name of the server, any other ideas?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Jimmy
  • 9,686
  • 14
  • 59
  • 78

7 Answers7

54

I got it working using the following:

string IP = Request.UserHostName;
string compName = CompNameHelper.DetermineCompName(IP);

code from compnamehelper:

public static string DetermineCompName(string IP)
{
    IPAddress myIP = IPAddress.Parse(IP);
    IPHostEntry GetIPHost = Dns.GetHostEntry(myIP);
    List<string> compName = GetIPHost.HostName.ToString().Split('.').ToList();
    return compName.First();
}
Aaron Hudon
  • 5,280
  • 4
  • 53
  • 60
Jimmy
  • 9,686
  • 14
  • 59
  • 78
  • 3
    Note that this will not give you the expected result if the client is behind a NAT, but chances are that it's not a problem since you're on an "intranet". – Niklas May 14 '10 at 10:37
  • It is working like a charm. But small change i had to make for getting the IP. string ip = HttpContext.Request.UserHostName.ToString(); rest are awesomely working.. +1 – Vinnie Aug 16 '13 at 20:48
  • Well, it may work only on local area network but it will not work on the internet. – Jamshaid K. Oct 07 '17 at 15:42
  • I just get this error "no such host is known" on the *Dns.GetHostEntry(myIP)*, is there any solution? – Farzad Karimi Jun 23 '20 at 08:20
4

code in VB :

Dim myIP As IPAddress = IPAddress.Parse(Request.UserHostName)
    Dim GetIPHost As IPHostEntry = Dns.GetHostEntry(myIP)
    Dim compName As List(Of String) = GetIPHost.HostName.ToString.Split("").ToList

    return(compName.First)
3

No. The client's computer name is not available in any way on the server. This is the nature of the http request-response. You only can have its IP address.

A workarounds could be to retrieve machine on the client from Flash/Silverlight (I doubt JavaScript) and put in into cookie which is available on the server with each request. But there is a whole stack of issues with this approach.

Dmytrii Nagirniak
  • 23,696
  • 13
  • 75
  • 130
1

I think you are better off using one of these methods to tie a user to a location:

  • a cookie that is set once the user self-selects their location
  • having the user login to the site so that you can track them uniquely that way
  • remembering user by IP address

There is no way of ensuring remote hostnames are unique. The same issue occurs with IP because of proxies, dynamic IP, etc., but I think it will be a little more reliable. Also, you can do geolocation by IP address.

D'Arcy Rittich
  • 167,292
  • 40
  • 290
  • 283
1

Try this:

string name = Request.UserHostName;
Matt Wrock
  • 6,590
  • 29
  • 23
0

The only way I know of to inspect the client is through the ServerVariables collection on the Request object (should be available for MVC code).

See https://web.archive.org/web/20210927201638/http://www.4guysfromrolla.com/webtech/092298-3.shtml for more information. REMOTE_HOST and REMOTE_ADDR look like candidates.

David Andres
  • 31,351
  • 7
  • 46
  • 36
-1

Here's an IE-only solution. It works in IE8, with multiple security warnings.

<script type="text/javascript" language="javascript">
   var ax = new ActiveXObject("WScript.Network");
   document.write(ax.UserName + '<br />'); //logged in account name
   document.write(ax.ComputerName + '<br />'); //Windows PC name
</script>
p.campbell
  • 98,673
  • 67
  • 256
  • 322