简体   繁体   English

在访问Web应用程序的服务器上获取客户端IP和主机名

[英]Getting clients IP and host name at server who hit to the web app

I have the following method but on some system it gives me all IP addresses ( IPv6 , IPv4 , and virtual IPv6 and virtual IPv4 ) and on a few system it gives me only IPv6 and virtual IPv6 but I need all IP's of client who hit the web-app to store in database. 我有以下方法,但在某些系统上,它可以为我提供所有IP地址( IPv6IPv4 ,虚拟IPv6和虚拟IPv4 ),在某些系统上,它只能为我提供IPv6和虚拟IPv6但是我需要客户端的所有IP's Web应用程序以存储在数据库中。

Is there a problem with the system configuration? 系统配置是否有问题?

public void Main()
{
    Username = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();

    computerName = System.Net.Dns.GetHostName(); 
    IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(computerName); 

    IPAddress[] ipAddress = ipEntry.AddressList; 
    IPv6 = ipAddress[0].ToString(); 

    IPv4 = ipAddress[2].ToString(); 
    VirtualIPv4 = ipAddress[3].ToString(); 

    IFormatProvider theCultureInfo = new System.Globalization.CultureInfo("en-GB",true); 

    dt1 = Convert.ToDateTime(DateTime.Now,theCultureInfo).ToString("MM-dd-yyyy hh:mm tt");
}

Conceptual points: 概念要点:

  1. Both servers and clients have IP addresses, that's how data gets sent (via IP datagrams) 服务器和客户端都有IP地址,这就是数据发送方式(通过IP数据报)
  2. Clients connect to a server using a single IP address (whether v4 or v6)... server binds to one or more IP addresses* 客户端使用单个IP地址(无论是v4还是v6)连接到服务器...服务器绑定到一个或多个 IP地址*

If you are still confused by this, see the bottom section 如果您仍然对此感到困惑,请参阅底部

So, since you are getting a list of IP addresses, you must be doing something wrong. 因此,由于获取的是IP地址列表,因此您必须做错了什么。 In this case, you are calling on the server a method to get the current IP address(es) of the server. 在这种情况下,您正在服务器上调用一种方法来获取服务器的当前IP地址。

Username = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();

The above is where you go wrong - everything after that is pointless. 上面是您出问题的地方-之后的一切都是毫无意义的。 You are running that code on the server, and WindowsIdentity.GetCurrent() is whoever is running the application. 您正在服务器上运行该代码,并且WindowsIdentity.GetCurrent()是运行应用程序的任何人。

Getting the client IP address 获取客户端IP地址

You want to get the Request IP address, which varies from framework to framework , but is something like HttpRequest.UserHostAddress 您想要获取Request IP地址,该地址因框架而异,但类似于HttpRequest.UserHostAddress

In ASP.NET, you can use: 在ASP.NET中,可以使用:

  1. HttpContext.Current.Request.UserHostAddress
  2. HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]

What about IPv4 vs v6? IPv4 vs v6呢? Not your concern - the client uses one or the other, and so whatever it is, it will be in that value. 与您无关,客户使用一个或另一个,因此无论它是什么,它都将具有该价值。

How to think about IP addressing wrt web apps 如何考虑WRT Web应用程序的IP寻址

You have two players in this world: a client, who wants to get to the web app, and a web app, who serves clients. 在这个世界上,您有两个参与者:一个想要访问Web应用程序的客户,另一个为客户服务的Web应用程序。

The way a client identifies himself and the way a server identifies himself is via some address, usually an IP address , unless you are using an exotic networking stack. 客户端标识自己和服务器标识自己的方式是通过某个地址(通常是IP address ,除非您使用的是外来的网络堆栈。

Now, there are two versions of IP that are common: IPv4, the more common and older version, which looks like 123.45.67.89 and the other one, which is hexadecimal and ugly. 现在,有两种常见的IP版本:IPv4,更常见的版本和较旧的版本,看起来像123.45.67.89 ,而另一种则是十六进制且丑陋的。

The address version corresponds to the IP version, the protocol version. 地址版本对应于IP版本,协议版本。 To communicate with me via IPv6, we need IPv6 addresses. 要通过IPv6与我通信,我们需要IPv6地址。 Likewise, to use IPv4, we need IPv4 addresses. 同样,要使用IPv4,我们需要IPv4地址。

So, to start, we need a server IP or IPs to connect to. 因此,开始时,我们需要一个或多个服务器IP进行连接。 If multiple IPs are available for different versions, we pick the highest version that is available on both ends, so if the server has v4 only, the client must communicate via v4 only. 如果多个IP可用于不同版本,我们将选择两端可用的最高版本,因此,如果服务器仅具有v4,则客户端必须仅通过v4进行通信。 If the client has both and the server is listening only on v6, then they will communicate via IPv6, and thus the client will use the IPv6 address to identify himself. 如果客户端同时拥有这两个客户端,并且服务器仅在v6上侦听,则它们将通过IPv6进行通信,因此客户端将使用IPv6地址来标识自己。

Finally, on both ends, keep in mind that IP addresses may not be the final word in identifying who is behind each end. 最后,在两端,请记住,IP地址可能不是确定谁在每个端点后面的最后一句话。 That is because of the potential (and often reality) of layers in between the nodes that technically violate the "end to end principle," but in practice are very very common. 这是因为节点之间的层可能(通常是现实)在技术上违反了“端到端原理”,但实际上非常普遍。 In these scenarios, an IP address may be shared by multiple private computers (most common), a set of IP addresses may be shared by a set of computers (less common, used for servers in round-robin configuration), a firewall in between may only permit traffic one direction or at certain times of day, or forward based on the sender IP, etc. 在这些情况下,一个IP地址可以由多台私有计算机共享(最常见),一组IP地址可以由一组计算机共享(不常见,用于循环配置中的服务器),在两者之间有防火墙根据发件人IP等,可能只允许一个方向或一天中的特定时间进行流量,或向前进行流量。

Some examples 一些例子

  • Port forwarding: You have an Internet connection using a cable modem that is connected to a firewall/router/wifi device. 端口转发:您使用连接至防火墙/路由器/ wifi设备的电缆调制解调器进行Internet连接。 Six different computers and phones connect to the firewall/router to share that one connection. 六台不同的计算机和电话连接到防火墙/路由器,以共享该连接。 They all share one IP, but traffic is routed back to them based on a port number assignment that is temporarily made by the firewall. 它们都共享一个IP,但是流量是根据防火墙临时分配的端口号分配回它们的。
  • NAT: This is a more general term for port forwarding and covers more exotic scenarios, but basically is a similar concept - you have one IP address (ingress) coming in, a firewall rewrites it (translation) so when you or the server or whoever gets it, it is translated to a routable address. NAT:这是端口转发的一个更通用的术语,涵盖了更多的特殊情况,但基本上是一个相似的概念-您有一个IP地址(入口)进入,防火墙将其重写(转换),因此无论您是服务器还是任何人得到它,它被翻译成可路由的地址。 On the way out (egress), it needs to be NAT'ted again, this time going from the IP addresses you understand to the IP addresses publicly-understood. 在出站(出口)时,需要再次进行NAT,这一次是从您了解的IP地址到公开理解的IP地址。 (Outbound NAT) (出站NAT)
  • Round robin: M computers share N IP addresses; 循环:M台计算机共享N个IP地址; something in between rotates who gets what IP. 中间的东西轮流谁获得了什么IP。

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

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