ASP.NET IP Address behind Proxy
While working on a Project where we needed to submit a monthly report to our clients which included the number of Clicks and number of Unique users, we always ended up having about 10-20% discrepancies when it was compared with the clients internal reports. Most of our clients were using Atlas tracking (Microsoft's product) which made us rethink the blame game. So one of our clients was kind enough to send us their internal report and we realized that they had more unique IP users (like 20% more users IP) logged than we did. So while brainstorming our code we found that we missed to code the obvious scenario when the User's are behind a proxy server like computers within a office n/w share the same IP.
March 2010
Using Request.ServerVariables["REMOTE_ADDR"] was only logging the Proxy address and not the actual clients IP. So we had to modify our code to the following:
String userIP = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
// If there is no proxy, get the standard remote address
if (userIP == null || userIP == "" || userIP.ToLower() == "unknown") {
userIP = Request.ServerVariables["REMOTE_ADDR"];
}
"HTTP_X_FORWARDED_FOR" request variable for the most part has the actual clients address but we need to make sure the variable is not null or empty. Thus the conditional check.