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

  • 相关阅读:
    mongodb简介
    tomcat简介
    date的详细说明
    30岁前成功的12条黄金法则
    2012春晚经典语录
    统计文件中某个单词出现的次数
    nginx简介
    ATM取款机系统模拟仿真
    十三种时间管理方法
    笔试常见的智力题 附答案
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/4440370.html
Copyright © 2011-2022 走看看