您的位置:首页 > 编程语言 > ASP

Asp.net获取用户真实Ip地址

2015-04-19 23:12 465 查看
/// <summary>
/// 获取远程访问用户的Ip地址
/// </summary>
/// <returns>返回Ip地址</returns>
protected string GetLoginIp()
{
string loginip = "";
//Request.ServerVariables[""]--获取服务变量集合
if (Request.ServerVariables["REMOTE_ADDR"] != null) //判断发出请求的远程主机的ip地址是否为空
{
//获取发出请求的远程主机的Ip地址
loginip = Request.ServerVariables["REMOTE_ADDR"].ToString();
}
//判断登记用户是否使用设置代理
else if (Request.ServerVariables["HTTP_VIA"] != null)
{
if (Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
{
//获取代理的服务器Ip地址
loginip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
}
else
{
//获取客户端IP
loginip = Request.UserHostAddress;
}
}
else
{
//获取客户端IP
loginip = Request.UserHostAddress;
}
return loginip;
}

if(Context.Request.ServerVariables["HTTP_VIA"]!=null) // using proxy
{
ip=Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); // Return real client IP.
}
else// not using proxy or can't get the Client IP
{
ip=Context.Request.ServerVariables["REMOTE_ADDR"].ToString(); //While it can't get the Client IP, it will return proxy IP.
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: