1 /// <summary> 2 /// 获得当前页面客户端的IP 3 /// </summary> 4 /// <returns>当前页面客户端的IP</returns> 5 public static string GetIP() 6 { 7 8 9 string result = String.Empty; 10 11 result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; 12 if (null == result || result == String.Empty) 13 { 14 result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; 15 } 16 17 if (null == result || result == String.Empty) 18 { 19 result = HttpContext.Current.Request.UserHostAddress; 20 } 21 22 if (null == result || result == String.Empty || !ValidateUtils.IsIP(result)) 23 { 24 return "0.0.0.0"; 25 } 26 27 return result; 28 29 }
转自:记住你 C#社区 http://www.jizhuni.com/thread-533-1-1.html