MCT Maulik Patel from India provides a server-side IP retrieval Solution
If (context. Request. servervariables ["http_via"]! = NULL) // Using proxy
{
IP = context. Request. servervariables ["http_x_forwarded_for"]. tostring (); // return real client IP.
}
Else // not using proxy or can't get the client IP
{
IP = context. Request. servervariables ["remote_addr"]. tostring (); // while it can't get the client IP, it will return proxy IP.
}
PS
1. Some proxies will not send us real IP addresses.
2. Some clients will not send us IP addresses because of the "header_access deny" security settings.
However, an error occurred while obtaining the real client IP address due to the second remark (the above solution is adjusted as follows)
The following solutions are available:
/// <Summary>
/// Obtain the remote IP Address
/// </Summary>
/// <Returns> IP address of the remote host </returns>
Public static string getcustomerip ()
{
String customerip = "";
If (httpcontext. Current. Request. servervariables ["http_via"]! = NULL) // Using proxy
{
Try
{
// Return real client IP.
Customerip = httpcontext. Current. Request. servervariables ["http_x_forwarded_for"]. tostring ();
}
Catch
{
// While it can't get the client IP, it will return proxy IP.
Customerip = httpcontext. Current. Request. servervariables ["remote_addr"]. tostring ();
}
}
Else
{
// While it can't get the client IP, it will return proxy IP.
Customerip = httpcontext. Current. Request. servervariables ["remote_addr"]. tostring ();
}
Return customerip;
}