Determine Client's Computer Name

谁都会走 提交于 2019-11-27 03:20:30
Jimmy

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

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)

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.

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.

Try this:

string name = Request.UserHostName;

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.

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>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!