Asp.net's request comes with a property request. userhostaddress for obtaining the Client IP address. However, the IP address obtained through userhostaddress cannot be authentic and accurate. What should I do if the client uses a proxy?
If (request. servervariables ["remote_addr"]! = NULL) // the IP address of the remote host that sends the request
{
This. IPaddress = request. servervariables ["remote_addr"]. tostring ();
}
Else if (request. servervariables ["http_via"]! = NULL) // determines whether a proxy is set. If a proxy is used
{
If (request. servervariables ["http_x_forwarded_for"]! = NULL) // obtain the IP address of the proxy server
{
This. IPaddress = request. servervariables ["http_x_forwarded_for"]. tostring ();
}
Else
{
This. IPaddress = request. userhostaddress;
}
}
Else
{
This. IPaddress = request. userhostaddress;
}
Use the preceding Code To obtain the Client IP address accurately.