从HttpRequestMessage对象获取IP地址
从HttpRequestMessage对象获取IP地址
C# Code:
/// <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;
}
//来源:C/S框架网(www.csframework.com) QQ:1980854898
/// <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;
}
//来源:C/S框架网(www.csframework.com) QQ:1980854898
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网