/// <summary>
///
获取客户端IP地址
///
</summary>
/// <param
name="request"></param>
///
<returns></returns>
public static string GetIpAddr(HttpRequest
request)
{
string
ip = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if
(string.IsNullOrEmpty(ip) || "unknown".Equals(ip,
StringComparison.CurrentCultureIgnoreCase))
{
ip =
request.ServerVariables["Proxy-Client-IP"];
}
if
(string.IsNullOrEmpty(ip) || "unknown".Equals(ip,
StringComparison.CurrentCultureIgnoreCase))
{
ip =
request.ServerVariables["WL-Proxy-Client-IP"];
}
if
(string.IsNullOrEmpty(ip) || "unknown".Equals(ip,
StringComparison.CurrentCultureIgnoreCase))
{
ip =
request.ServerVariables["HTTP_CLIENT_IP"];
}
if
(string.IsNullOrEmpty(ip) || "unknown".Equals(ip,
StringComparison.CurrentCultureIgnoreCase))
{
ip =
request.ServerVariables["REMOTE_ADDR"];
}
if
(string.IsNullOrEmpty(ip) || "unknown".Equals(ip,
StringComparison.CurrentCultureIgnoreCase))
{
ip = "";
}
if
(!string.IsNullOrEmpty(ip) && ip.IndexOf(",", StringComparison.Ordinal)
!= -1 && ip.Length >
6)
{
ip =
ip.Split(‘,‘)[0];
}
return
ip;
}
原文:http://www.cnblogs.com/yuncode/p/3569133.html