[英]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
地址( IPv6
, IPv4
,虚拟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: 概念要点:
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()
是运行应用程序的任何人。
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中,可以使用:
HttpContext.Current.Request.UserHostAddress
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.
与您无关,客户使用一个或另一个,因此无论它是什么,它都将具有该价值。
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 一些例子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.