C#.NET IP地址工具类WebAPI服务器使用
C#.NET IP地址工具类WebAPI服务器使用
C# Code:
/// <summary>
/// IP地址工具类
/// </summary>
public static class IPHelper
{
/// <summary>
/// 获取IP地址
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public static string GetIPAddress(HttpRequestMessage request)
{
string ip = "";
if (request.Properties.ContainsKey("MS_OwinContext"))
{
ip = ((OwinContext)request.Properties["MS_OwinContext"]).Request.RemoteIpAddress;
}
else if (request.Properties.ContainsKey("MS_HttpContext"))
{
ip = ((HttpContextWrapper)request.Properties["MS_HttpContext"]).Request.UserHostAddress;
}
else if (request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name))
{
ip = ((RemoteEndpointMessageProperty)request.Properties[RemoteEndpointMessageProperty.Name]).Address;
}
if (ip == "::1") ip = "127.0.0.1";
if (string.IsNullOrEmpty(ip) || !IsIP(ip))
return "127.0.0.1";
else
return ip;
}
/// <summary>
/// 获取IP地址
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public static string GetIPAddress(HttpRequest request)
{
string result = String.Empty;
result = request.ServerVariables["HTTP_CDN_SRC_IP"];
if (string.IsNullOrEmpty(result))
result = request.ServerVariables["REMOTE_ADDR"];
if (string.IsNullOrEmpty(result))
result = request.UserHostAddress;
if (string.IsNullOrEmpty(result) || !IsIP(result))
return "127.0.0.1";
return result;
}
/// <summary>
/// 是否IP
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
public static bool IsIP(string ip)
{
return System.Text.RegularExpressions.Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
}
}
//来源:C/S框架网(www.csframework.com) QQ:1980854898
/// <summary>
/// IP地址工具类
/// </summary>
public static class IPHelper
{
/// <summary>
/// 获取IP地址
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public static string GetIPAddress(HttpRequestMessage request)
{
string ip = "";
if (request.Properties.ContainsKey("MS_OwinContext"))
{
ip = ((OwinContext)request.Properties["MS_OwinContext"]).Request.RemoteIpAddress;
}
else if (request.Properties.ContainsKey("MS_HttpContext"))
{
ip = ((HttpContextWrapper)request.Properties["MS_HttpContext"]).Request.UserHostAddress;
}
else if (request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name))
{
ip = ((RemoteEndpointMessageProperty)request.Properties[RemoteEndpointMessageProperty.Name]).Address;
}
if (ip == "::1") ip = "127.0.0.1";
if (string.IsNullOrEmpty(ip) || !IsIP(ip))
return "127.0.0.1";
else
return ip;
}
/// <summary>
/// 获取IP地址
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public static string GetIPAddress(HttpRequest request)
{
string result = String.Empty;
result = request.ServerVariables["HTTP_CDN_SRC_IP"];
if (string.IsNullOrEmpty(result))
result = request.ServerVariables["REMOTE_ADDR"];
if (string.IsNullOrEmpty(result))
result = request.UserHostAddress;
if (string.IsNullOrEmpty(result) || !IsIP(result))
return "127.0.0.1";
return result;
}
/// <summary>
/// 是否IP
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
public static bool IsIP(string ip)
{
return System.Text.RegularExpressions.Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
}
}
//来源:C/S框架网(www.csframework.com) QQ:1980854898
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网