11
  1. In my Dev Machine HttpContext.Current.Request.UserHostAddress is null. Why? how can I turn it on?
  2. How can I get list of Ips in case of a proxy client?

WCF Service with ASP.net 4 window7.

Thanks

BFree
  • 102,548
  • 21
  • 159
  • 201
SexyMF
  • 10,657
  • 33
  • 102
  • 206

1 Answers1

8

to avoid this problem you can parse the HTTP_X_FORWARDED_FOR for the last entery IP.

ip=Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ;
if (!string.IsNullOrEmpty(ip))
{
   string[] ipRange = ip.Split(',');
   int le = ipRange.Length - 1;
   string trueIP = ipRange[le];
}
else
{
   ip=Request.ServerVariables["REMOTE_ADDR"];
}

Hope this will helps you

Viral Sarvaiya
  • 781
  • 2
  • 9
  • 29
  • 4
    Just a note about `Viral Sarvaiya`'s answer. According to [Wikipedia](http://en.wikipedia.org/wiki/X-Forwarded-For) and [Amazon](http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/TerminologyandKeyConcepts.html) the X-Forwarded-For header has the following format: > X-Forwarded-For: client, proxy1, proxy2 This means you should be using the first string in `ipRange`, not the last. – dubrowgn Mar 23 '15 at 17:23