Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpContext.Current.Request.UserHostAddress is null

Tags:

c#

ip-address

  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

like image 763
SexyMF Avatar asked Oct 06 '11 13:10

SexyMF


1 Answers

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

like image 143
Viral Sarvaiya Avatar answered Sep 23 '22 16:09

Viral Sarvaiya