C#获得当前页面客户端的IP(C#获取客户端真实IP各种情况都适用) 

用于监测获取某个页面上用户的客户端真实IP地址 

cdn上再也不怕获取不到客户的真实IP地址了 


C#代码:


       /// <summary>
       /// 获得当前页面客户端的IP
       /// </summary>
       /// <returns>当前页面客户端的IP</returns>
       public static string GetIP()
       {
           string result = String.Empty;
  
           result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
           if (null == result || result == String.Empty)
           {
               result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
           }
  
           if (null == result || result == String.Empty)
           {
               result = HttpContext.Current.Request.UserHostAddress;
           }
  
           if (null == result || result == String.Empty || !ValidateUtils.IsIP(result))
           {
               return "0.0.0.0";
           }
  
           return result;
  
       }