C# 确定客户端的计算机名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1444592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 16:36:27  来源:igfitidea点击:

Determine Client's Computer Name

c#asp.net-mvc

提问by Jimmy

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 但它只返回服务器的名称,还有其他想法吗?

采纳答案by Jimmy

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

回答by David Andres

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.shtmlfor more information. REMOTE_HOST and REMOTE_ADDR look like candidates.

有关更多信息,请参阅http://www.4guysfromrolla.com/webtech/092298-3.shtml。REMOTE_HOST 和 REMOTE_ADDR 看起来像候选对象。

回答by RedFilter

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
  • 用户自行选择位置后设置的 cookie
  • 让用户登录到该站点,以便您可以通过这种方式唯一地跟踪他们
  • 通过 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. Also, you can do geolocation by IP address.

无法确保远程主机名是唯一的。由于代理,动态IP等,IP也会出现同样的问题,但我认为它会更可靠一些。此外,您可以通过 IP 地址进行地理定位。

回答by Dmytrii Nagirniak

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.

否。客户端的计算机名称在服务器上以任何方式都不可用。这就是 http 请求-响应的本​​质。您只能拥有其 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. But there is a whole stack of issues with this approach.

一种解决方法可能是从 Flash/Silverlight(我怀疑 JavaScript)检索客户端上的机器,并将其放入服务器上随每个请求提供的 cookie 中。但是这种方法有很多问题。

回答by Matt Wrock

Try this:

尝试这个:

string name = Request.UserHostName;

回答by p.campbell

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

这是一个仅限 IE 的解决方案。它适用于 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>

回答by hosein kardoost

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)