#region Ip(客戶端IP地址)
/// < summary >
/// 客戶端IP地址
/// </ summary >
public static string Ip
{
get
{
var result = string.Empty;
if (HttpContext.Current != null)
{
result = GetWebClientIp();
}
if (string.IsNullOrWhiteSpace(result))
{
result = GetLanIp();
}
return result;
}
}
/// < summary >
/// 獲取Web客戶端的IP
/// </ summary >
/// < returns ></ returns >
private static string GetWebClientIp()
{
var ip = GetWebProxyRealIp() ?? GetWebRemoteIp();
foreach (var hostAddress in Dns.GetHostAddresses(ip))
{
if (hostAddress.AddressFamily == AddressFamily.InterNetwork)
{
return hostAddress.ToString();
}
}
return string.Empty;
}
/// < summary >
/// 獲取Web遠程IP
/// </ summary >
/// < returns ></ returns >
private static string GetWebRemoteIp()
{
try
{
return HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ??
HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"] ?? "";
}
catch (Exception e)
{
return string.Empty;
}
}
/// < summary >
/// 獲取Web代理真實IP
/// </ summary >
/// < returns ></ returns >
private static string GetWebProxyRealIp()
{
var request = HttpContext.Current.Request;
string ip = request.Headers.Get("x-forwarded-for");
if (string.IsNullOrEmpty(ip) || string.Equals("unknown", ip, StringComparison.OrdinalIgnoreCase))
{
ip = request.Headers.Get("Proxy-Client-IP");
}
if (string.IsNullOrEmpty(ip) || string.Equals("unknown", ip, StringComparison.OrdinalIgnoreCase))
{
ip = request.Headers.Get("WL-Proxy-Client-IP");
}
if (string.IsNullOrEmpty(ip) || string.Equals("unknown", ip, StringComparison.OrdinalIgnoreCase))
{
ip = request.UserHostAddress;
}
if (string.IsNullOrEmpty(ip))
{
return string.Empty;
}
// 可能存在如下格式:X-Forwarded-For: client, proxy1, proxy2
if (ip.Contains(", "))
{
// 如果存在多個反向代理,獲得的IP是一個用逗號分隔的IP集合,取第一個
// X-Forwarded-For: client 第一個
string[] ips = ip.Split(new string[1] {", "}, StringSplitOptions.RemoveEmptyEntries);
var i = 0;
for (i = 0; i < ips.Length ; i++)
{
if (ips[i] != "")
{
// 判斷是否為內網IP
if (false == IsInnerIp(ips[i]))
{
IPAddress realIp;
if (IPAddress.TryParse(ips[i], out realIp) && ips[i].Split('.').Length == 4)
{
//合法IP
return ips[i];
}
return "";
}
}
}
ip = ips [0];// 默認獲取第一個IP地址
}
return ip;
}
/// <summary>
/// 判斷IP地址是否為內網IP地址
/// </ summary >
/// < param name = "ip" >IP地址</ param >
/// < returns ></ returns >
private static bool IsInnerIp(string ip)
{
bool isInnerIp = false;
ulong ipNum = Ip2Ulong(ip);
/**
* 私有IP
* A類:10.0.0.0-10.255.255.255
* B類:172.16.0.0-172.31.255.255
* C類:192.168.0.0-192.168.255.255
* 當然,還有127這個網段是環回地址
*/
ulong aBegin = Ip2Ulong("10.0.0.0");
ulong aEnd = Ip2Ulong("10.255.255.255");
ulong bBegin = Ip2Ulong("172.16.0.0");
ulong bEnd = Ip2Ulong("10.31.255.255");
ulong cBegin = Ip2Ulong("192.168.0.0");
ulong cEnd = Ip2Ulong("192.168.255.255");
isInnerIp = IsInner(ipNum, aBegin, aEnd) || IsInner(ipNum, bBegin, bEnd) || IsInner(ipNum, cBegin, cEnd) ||
ip.Equals("127.0.0.1");
return isInnerIp;
}
/// < summary >
/// 將IP地址轉換為Long型數字
/// </ summary >
/// < param name = "ip" >IP地址</ param >
/// < returns ></ returns >
private static ulong Ip2Ulong(string ip)
{
byte[] bytes = IPAddress.Parse(ip).GetAddressBytes();
ulong ret = 0;
foreach (var b in bytes)
{
ret <<= 8;
ret |= b;
}
return ret;
}
/// < summary >
/// 判斷用戶IP地址轉換為Long型后是否在內網IP地址所在范圍
/// </ summary >
/// < param name = "userIp" >用戶IP</ param >
/// < param name = "begin" >開始范圍</ param >
/// < param name = "end" >結束范圍</ param >
/// < returns ></ returns >
private static bool IsInner(ulong userIp, ulong begin, ulong end)
{
return (userIp >= begin) && (userIp <= end);
}
/// < summary >
/// 獲取局域網IP
/// </ summary >
/// < returns ></ returns >
private static string GetLanIp()
{
foreach (var hostAddress in Dns.GetHostAddresses(Dns.GetHostName()))
{
if (hostAddress.AddressFamily == AddressFamily.InterNetwork)
{
return hostAddress.ToString();
}
}
return string.Empty;
}
#endregion
|