首页 新闻 会员 周边

B/S获取用户登陆地址的IP

0
[已解决问题] 解决于 2015-08-10 21:05

  如题获取用户IP,网上面找了半天都没卵用,在服务器上测试了也没用,求代码

赶猪上架的主页 赶猪上架 | 初学一级 | 园豆:6
提问于:2015-07-02 15:51
< >
分享
最佳答案
1

public static string GetUserIp()
{
string realRemoteIp = string.Empty;
if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
{
realRemoteIp =
System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].Split(',')[0];
}
if (string.IsNullOrEmpty(realRemoteIp))
{
realRemoteIp = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
if (string.IsNullOrEmpty(realRemoteIp))
{
realRemoteIp = System.Web.HttpContext.Current.Request.UserHostAddress;
}
return realRemoteIp;
}

奖励园豆:5
junjieok | 小虾三级 |园豆:779 | 2015-07-02 19:17
其他回答(3)
0

if(Context.Request.ServerVariables["HTTP_VIA"]!=null) 

{   

  ip=Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();

}

 else

{           

  ip=Context.Request.ServerVariables["REMOTE_ADDR"].ToString(); 

}

MrNice | 园豆:3450 (老鸟四级) | 2015-07-02 16:27
0

Request.UserHostAddress

dudu | 园豆:30994 (高人七级) | 2015-07-02 17:14
0

你确定你部署的时候是没有通过反向代理比如nginx或者apache的吗??如果通过这两个,需要在他们上面配置,不然就是127.0.0.1

洛城秋色 | 园豆:324 (菜鸟二级) | 2015-07-03 16:04
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册