HttpContext.Current.Request.UserHostAddress is nul

2019-04-21 05:16发布

  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

标签: c# ip-address
1条回答
一纸荒年 Trace。
2楼-- · 2019-04-21 06:05

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

查看更多
登录 后发表回答