There are bugs in the so-called "getting real IP addresses" method on the Internet, and the multi-layer transparent proxy is not taken into account.
Most codes are similar:
String IPaddress = (httpcontext. Current. Request. servervariables ["http_x_forwarded_for"]! = NULL
& Httpcontext. Current. Request. servervariables ["http_x_forwarded_for"]! = String. Empty)
? Httpcontext. Current. Request. servervariables ["http_x_forwarded_for"]
: Httpcontext. Current. Request. servervariables ["remote_addr"];
In fact, the above Code only tries to use a layer-1 proxy with the user. if the user has a layer-2, layer-3 http_x_forwarded_for value is: "The real IP address of the Local Machine, layer-1 proxy IP address, layer-2 proxy IP address ,..... ", if the length of the IP field stored in your data is very small (15 bytes), the database reports an error.
In practice, there are few users who use multi-layer transparent proxy.
In other cases, more and more websites are using proxy acceleration. For example, Sina and Sohu news all use squid as proxy and use multiple servers for traffic distribution. Squid itself is similar to a transparent proxy. It will send "http_x_forwarded_for". http_x_forwarded_for includes the customer's IP address. If the customer already uses a transparent proxy, the "http_x_forwarded_for" obtained by the program includes two IP addresses. (I have encountered 3 IP addresses and 4 have not)
Therefore, to obtain the "real" IP address, you should also determine whether "," comma "exists in" http_x_forwarded_for ", or whether the length is too long (more than 15 bytes of XXX. XXX ).
The Code should be as follows:
/** // <Summary>
/// Obtain the real IP address of the client. If a proxy exists, the first non-Intranet address is used.
/// </Summary>
Public static string IPaddress
{
Get
{
String result = string. empty;
Result = httpcontext. Current. Request. servervariables ["http_x_forwarded_for"];
If (result! = NULL & result! = String. Empty)
{
// A proxy may exist.
If (result. indexof (".") =-1) // No "." is definitely not in IPv4 format
Result = NULL;
Else
{
If (result. indexof (",")! =-1)
{
// There are ",", multiple proxies are estimated. Obtain the first IP address that is not an intranet IP address.
Result = result. Replace ("", ""). Replace (""","");
String [] temparyip = result. Split (",;". tochararray ());
For (INT I = 0; I <temparyip. length; I ++)
{
If (text. isipaddress (temparyip [I])
& Temparyip [I]. substring (0, 3 )! = "10 ."
& Temparyip [I]. substring (0, 7 )! = "192.168"
& Temparyip [I]. substring (0, 7 )! = "172.16 .")
{
Return temparyip [I]; // locate an address that is not an intranet address
}
}
}
Else if (text. isipaddress (result) // The proxy is in the IP Format.
Return result;
Else
Result = NULL; // the content in the proxy is not an IP address and the IP address is used.
}
}
String IPaddress = (httpcontext. Current. Request. servervariables ["http_x_forwarded_for"]! = NULL & httpcontext. Current. Request. servervariables ["http_x_forwarded_for"]! = String. Empty )? Httpcontext. Current. Request. servervariables ["http_x_forwarded_for"]: httpcontext. Current. Request. servervariables ["remote_addr"];
If (null = Result | result = string. Empty)
Result = httpcontext. Current. Request. servervariables ["remote_addr"];
If (result = NULL | result = string. Empty)
Result = httpcontext. Current. Request. userhostaddress;
Return result;
}
}