简体   繁体   English

如何在 ASP.NET / C# 中获取客户端计算机名称?

[英]How to get Client Machine Name in ASP.NET / C#?

I have application want to get user machine name, here I can retrieve and it works fine in localhost.我有应用程序想要获取用户机器名,在这里我可以检索并且它在 localhost 中工作正常。

string clientPCName;
string[] computer_name = System.Net.Dns.GetHostEntry(
Request.ServerVariables["remote_host"]).HostName.Split(new Char[] { '.' });
clientPCName = computer_name[0].ToString(); 

In local it returns exactly my computer name.在本地,它准确地返回我的计算机名称。
But on server I get result like this: 0x57364794但在服务器上我得到这样的结果: 0x57364794
Any solution?有什么解决办法吗?

Try this it works for me试试这个它对我有用

string clientMachineName;
clientMachineName = (Dns.GetHostEntry(Request.ServerVariables["remote_addr"]).HostName);
Response.Write(clientMachineName);

Machine names are a concept for local networks only, and local AD networks/forests at that.机器名称仅适用于本地网络,以及本地 AD 网络/森林。 Once you expose your site on the internet machine names no longer make sense, instead you get DNS names.一旦您在 Internet 上公开您的站点,机器名称就不再有意义,而是您会得到 DNS 名称。

So, for example, on my the work network my laptop name resolves to bdorrans01.redmond.contoso.com.例如,在我的工作网络上,我的笔记本电脑名称解析为 bdorrans01.redmond.contoso.com。 But if I browse to a web site from home my current ip address resolves to c-98-203-143-14.hsd1.wa.comcast.net.但是,如果我从家里浏览到 web 站点,我当前的 ip 地址将解析为 c-98-203-143-14.hsd1.wa.comcast.net。

You are taking the FQDN and dropping everything after the first period/full stop.您正在使用 FQDN 并在第一个句号/句号之后删除所有内容。 That works in an AD environment (sort of), and your code would see bdorrans01 internally, but it would see c-98-203-143-14 externally.这适用于 AD 环境(有点),您的代码会在内部看到 bdorrans01,但在外部会看到 c-98-203-143-14。

Even internally within a multi-forest AD it would also be wrong if your web site was hosted in another forest, for example if you site lived on example.europe.contoso.com, your code would get a machine name of bdorrans01, but my machine wouldn't be in the same forest, and if you tried to ping it, or map to a network drive it wouldn't be found, because you would be looking for bdorrans01 in europe.contoso.com, not redmond.contoso.com where it actually is.即使在多林 AD 内部,如果您的 web 站点托管在另一个林中,例如,如果您的站点位于 example.europe.contoso.com 上,那么您的代码将获得一个机器名称 bdorrans01,但我的机器不会在同一个森林中,如果您尝试 ping 它,或尝试将 map 连接到网络驱动器,则找不到它,因为您将在 europe.contoso.com 中寻找 bdorrans01,而不是 redmond.contoso。 com 实际在哪里。

Also your code assumes that resolving an IP address will produce a fully qualified domain name, this isn't true in a lot of cases, you could just get an IP address back, so you may see 1.2.3.4 and your code would give a machine name of 1. Worse yet it'll not do anything to an IPv6 address as they use colons to separate, 2001:db8:85a3:8d3:1319:8a2e:370:7348此外,您的代码假设解析 IP 地址将产生一个完全限定的域名,这在很多情况下并非如此,您可以返回一个 IP 地址,因此您可能会看到 1.2.3.4 并且您的代码会给出机器名称为 1。更糟糕的是,它不会对 IPv6 地址做任何事情,因为它们使用冒号分隔,2001:db8:85a3:8d3:1319:8a2e:370:7348

So your premise is incorrect - and you need to find a different way to do what you're trying to do.所以你的前提是不正确的——你需要找到一种不同的方式来做你想做的事情。 Why do you believe you need the machine name anyway?为什么你认为你仍然需要机器名称?

As @blowdark wrote it's impossible actually to get client machine name and host name is not client machine name.正如@blowdark 所写,实际上不可能获得客户端机器名称,而主机名不是客户端机器名称。 Let's look at what from request you can have?让我们看看您可以从请求中获得什么?

  • Request IP address (TCP/IP)请求 IP 地址 (TCP/IP)
  • HTTP headers HTTP 接头

That's all.就这样。 IP address is an IP which is given by your Internet provider, and host name is name of some provider machine which Internet traffic passes through. IP 地址是您的 Internet 提供商提供的 IP,主机名是 Internet 流量通过的某些提供商机器的名称。 You even cannot get in simple way user machine local IP address in Internet provider network.您甚至无法在 Internet 提供商网络中以简单的方式获取用户机器本地 IP 地址。

If your application has load balancer or proxy then you need to read HTTP_X_FORWARDED_FOR header (by default) to get request client IP.如果您的应用程序有负载均衡器或代理,那么您需要阅读 HTTP_X_FORWARDED_FOR header(默认)以获取请求客户端 IP。 But it's provider IP again.但它又是供应商 IP。

So, from request IP you cannot get real client machine name in general.因此,从请求 IP 通常您无法获得真实的客户端机器名称。 In HTTP headers browsers also don't pass machine name.在 HTTP 标头中,浏览器也不传递机器名称。 Perhaps with some kind of hacks you can get machine name asking provider server, but that's out of scope.也许通过某种黑客攻击,您可以获得询问提供商服务器的机器名称,但这不在 scope 范围内。

Once you enable browser to allow creation of active-x objects, for IE you can write below javascript code to get client computer name as:一旦您启用浏览器以允许创建 active-x 对象,对于 IE,您可以在 javascript 代码下方编写以获取客户端计算机名称:

var WinNetwork = new ActiveXObject("WScript.Network");
var ComputerName = WinNetwork.ComputerName;

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

相关问题 如何在 ASP.NET 中记录客户端 IP 地址和机器名称 - How to log Client IP Address and Machine Name in ASP.NET 如何使用 C# Asp.net 将水晶报表直接打印到客户端机器 - How to print crystal report directly to a client machine using C# Asp.net 如何获取客户端计算机的窗口原理ID(asp.net) - How to get window principle id of client machine (asp.net) 如何在asp.net中获取客户端机器的临时路径? - How to get temporary path of client machine in asp.net? 如何在c#和asp.net中获取客户端主机名,本地计算机名和用户名 - How I can obtain client hostname, local computer name and user name in c# and asp.net 从客户端获取唯一标识符ASP.Net c# - Get Unique identifier from client ASP.Net c# 如何从asp:FileUpload获取文件名并使用ASP.NET和C#显示在标签中 - How to get file name from asp:FileUpload and display in a Label using ASP.NET and C# 如何在asp.net核心中调用c#方法的ajax上获取当前主机名和路径名 - How to get current host name and path name on ajax calling c# method in asp.net core asp.net C#:获取委托中的属性名称 - asp.net C# : Get the name of property in delegate 在ASP.NET C#中获取域名和/或子站点名称 - Get domain and/ or subsite name in asp.net c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM