When your server is behind any proxies or load balancers, the remote IP@ gets masked. In such scenarios, the server variable “REMOTE_ADDR” may not provide the correct client IP@. The only way to get remote addresses is to check if the proxies rewrite the request headers which most of them does.
You can look through all HTTP request headers and try to find any “X_” or “HTTP_X” type value to get the desired IP@.
Some of HTTP request headers you can scan (in sequence) are given below:
- HTTP_X_CLIENTIP
- HTTP_CLIENT_IP
- HTTP_X_FORWARDED_FOR
- HTTP_X_FORWARDED
- HTTP_VIA
- HTTP_FORWARDED_FOR
- HTTP_FORWARDED
- REMOTE_ADDR
Sample C# implementation:
public static string GetClientIPAddress() { string strIpAddress = ""; strIpAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_CLIENTIP"]; if (!string.IsNullOrEmpty(strIpAddress)) { string[] ipRange = strIpAddress.Split(','); strIpAddress = ipRange[0].Trim(); } else { strIpAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; } return strIpAddress; }
(Visited 366 times, 1 visits today)