views:

1074

answers:

8

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?

A: 

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 http://www.4guysfromrolla.com/webtech/092298-3.shtml for more information. REMOTE_HOST and REMOTE_ADDR look like candidates.

David Andres
A: 

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.

RedFilter
+2  A: 

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
is there a method to lookup computer names with an ip?
Jimmy
You can get computer name from its IP by making a call to DNS server
Mahesh Velaga
+1  A: 

You could use the Request.LogonUserIdentity.Name property, which returns "ComputerName\Username", and then split the value on '\'.

Manticore
users are on a domain so they just show as domain/username so this is a problem
Jimmy
Ah, that's too bad!
Manticore
A: 

Dup question, check this out also, besides the answers here: http://stackoverflow.com/questions/922476/is-there-a-way-to-read-the-clients-machine-computer-name-from-the-browser

Dale Ragan
+1  A: 

Try this:

string name = Request.UserHostName;
Matt Wrock
A: 

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
+3  A: 

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();
    }
Jimmy
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