zoukankan      html  css  js  c++  java
  • Asp.net获取用户真实Ip地址

    /// <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. 
    }

  • 相关阅读:
    数据类型
    表达式
    类型
    go杂货铺
    rest framework
    go 学习之路(三)
    go 学习之路(二)
    文件管理之字符处理命令,打包压缩
    文件管理之文件查找,上传下载,输出重定向
    文本命令之三剑客初探
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/4440370.html
Copyright © 2011-2022 走看看