简体   繁体   English

确定客户端的计算机名称

[英]Determine Client's Computer Name

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? 我正在构建一个Intranet站点,该站点将根据计算机名称显示不同的列表,因为不同的计算机位于不同的区域,是否可以(在控制器或型号内)确定客户端的计算机名称?

I have tried system.environment.machinename but that only returns the name of the server, any other ideas? 我已经尝试过system.environment.machinename,但那只会返回服务器的名称,还有其他想法吗?

I got it working using the following: 我使用以下方法使其工作:

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

code from compnamehelper: 来自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 : 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. 这就是http请求-响应的性质。 You only can have its IP address. 您只能拥有其IP地址。

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. 一种解决方法是从Flash / Silverlight(我怀疑JavaScript)在客户端上检索计算机,然后将其放入cookie,该cookie随每个请求在服务器上可用。 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 用户自选位置后设置的cookie
  • having the user login to the site so that you can track them uniquely that way 让用户登录到站点,以便您可以通过这种方式唯一地跟踪他们
  • remembering user by IP address 通过IP地址记住用户

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. 由于代理,动态IP等原因,IP也会出现相同的问题,但我认为它将更加可靠。 Also, you can do geolocation by IP address. 另外,您可以按IP地址进行地理位置定位。

尝试这个:

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). 我知道检查客户端的唯一方法是通过Request对象上的ServerVariables集合(应可用于MVC代码)。

See http://www.4guysfromrolla.com/webtech/092298-3.shtml for more information. 有关更多信息,请参见http://www.4guysfromrolla.com/webtech/092298-3.shtml REMOTE_HOST and REMOTE_ADDR look like candidates. REMOTE_HOST和REMOTE_ADDR看起来像候选对象。

Here's an IE-only solution. 这是仅IE的解决方案。 It works in IE8, with multiple security warnings. 它可以在IE8中运行,并带有多个安全警告。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM