private static string GetClientIPAddress(out ClientIPVariable ipVar) { string str = null; ipVar = ClientIPVariable.None; if (string.Compare(HttpContext.Current.Request.Url.Scheme, "https", true) != 0) { if (!string.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"])) { ipVar = ClientIPVariable.Http_X_Forwarded_For; str = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].Split(",;".ToCharArray())[0]; } else if (!string.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables["HTTP_CLIENT_IP"])) { ipVar = ClientIPVariable.Http_Client_IP; str = HttpContext.Current.Request.ServerVariables["HTTP_CLIENT_IP"]; } else if (!string.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables["X_CLIENT_IP"])) { ipVar = ClientIPVariable.X_Client_IP; str = HttpContext.Current.Request.ServerVariables["X_CLIENT_IP"]; } } if ((str == null) && !string.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"])) { ipVar = ClientIPVariable.Remote_Addr; str = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; } return(str); }
/// <summary> /// 获取客户端IP真实地址 /// </summary> /// <param name="ipVar">IP变量的来源 (out 接收)</param> /// <returns>客户端IP真实地址</returns> private static string GetClientIPAddress(out ClientIPVariable ipVar) { string retValue = null; ipVar = ClientIPVariable.None; // 非加密页就检查 Proxy IP 变数 if (String.Compare(HttpContext.Current.Request.Url.Scheme, "https", true) != 0) { if (!string.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"])) { ipVar = ClientIPVariable.Http_X_Forwarded_For; retValue = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].Split(",;".ToCharArray())[0]; } else if (!string.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables["HTTP_CLIENT_IP"])) { ipVar = ClientIPVariable.Http_Client_IP; retValue = HttpContext.Current.Request.ServerVariables["HTTP_CLIENT_IP"]; } else if (!string.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables["X_CLIENT_IP"])) { ipVar = ClientIPVariable.X_Client_IP; retValue = HttpContext.Current.Request.ServerVariables["X_CLIENT_IP"]; } } // 若还没取到 IP if (retValue == null) { if (!string.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"])) { ipVar = ClientIPVariable.Remote_Addr; retValue = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; } } return(retValue); }