1 /// <summary> 2 /// 获取用户IP地址 3 /// </summary> 4 /// <returns></returns> 5 public static string GetClientIP() 6 { 7 8 string user_IP = string.Empty; 9 if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null) 10 { 11 if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null) 12 { 13 user_IP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); 14 } 15 else 16 { 17 user_IP = System.Web.HttpContext.Current.Request.UserHostAddress; 18 } 19 } 20 else 21 { 22 user_IP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString(); 23 } 24 return user_IP; 25 } 26