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

  • 相关阅读:
    【C++clock()函数学习(计算自己代码运行时间)】
    YCOJ 1041113【最近的回文数】
    计蒜客【汉诺塔II】
    YCOJ【汉诺塔】
    【常用算法总结——递归】
    YCOJ【查找】
    【常用算法总结——分治】
    Redis哨兵机制
    Redis主从复制
    SpringBoot集成Redis
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/4440370.html
Copyright © 2011-2022 走看看