您好我需要获取在web api中请求某种方法的客户端IP, 我试图使用here中的代码,但它总是返回服务器本地IP, 如何以正确的方式获得?
HttpContext.Current.Request.UserHostAddress;
来自其他问题:
public static class HttpRequestMessageExtensions
{
private const string HttpContext = "MS_HttpContext";
private const string RemoteEndpointMessage = "System.ServiceModel.Channels.RemoteEndpointMessageProperty";
public static string GetClientIpAddress(this HttpRequestMessage request)
{
if (request.Properties.ContainsKey(HttpContext))
{
dynamic ctx = request.Properties[HttpContext];
if (ctx != null)
{
return ctx.Request.UserHostAddress;
}
}
if (request.Properties.ContainsKey(RemoteEndpointMessage))
{
dynamic remoteEndpoint = request.Properties[RemoteEndpointMessage];
if (remoteEndpoint != null)
{
return remoteEndpoint.Address;
}
}
return null;
}
}
答案 0 :(得分:107)
以下链接可能会对您有所帮助。以下是来自以下链接的代码。
参考:getting-the-client-ip-via-asp-net-web-api
using System.Net.Http;
using System.ServiceModel.Channels;
using System.Web;
using System.Web.Http;
namespace Trikks.Controllers.Api
{
public class IpController : ApiController
{
public string GetIp()
{
return GetClientIp();
}
private string GetClientIp(HttpRequestMessage request = null)
{
request = request ?? Request;
if (request.Properties.ContainsKey("MS_HttpContext"))
{
return ((HttpContextWrapper)request.Properties["MS_HttpContext"]).Request.UserHostAddress;
}
else if (request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name))
{
RemoteEndpointMessageProperty prop = (RemoteEndpointMessageProperty)request.Properties[RemoteEndpointMessageProperty.Name];
return prop.Address;
}
else if (HttpContext.Current != null)
{
return HttpContext.Current.Request.UserHostAddress;
}
else
{
return null;
}
}
}
}
另一种方法是在下面。
参考:how-to-access-the-client-s-ip-address
适用于网络托管版
string clientAddress = HttpContext.Current.Request.UserHostAddress;
自我托管
object property;
Request.Properties.TryGetValue(typeof(RemoteEndpointMessageProperty).FullName, out property);
RemoteEndpointMessageProperty remoteProperty = property as RemoteEndpointMessageProperty;
答案 1 :(得分:67)
使用Web API 2.2:Request.GetOwinContext().Request.RemoteIpAddress
答案 2 :(得分:15)
尝试使用
获取IPip = HttpContext.Current != null ? HttpContext.Current.Request.UserHostAddress : "";
答案 3 :(得分:7)
如果您使用OWIN Self-host NuGet包自行托管Asp.Net 2.1,您可以使用以下代码:
private string getClientIp(HttpRequestMessage request = null)
{
if (request == null)
{
return null;
}
if (request.Properties.ContainsKey("MS_OwinContext"))
{
return ((OwinContext) request.Properties["MS_OwinContext"]).Request.RemoteIpAddress;
}
return null;
}
答案 4 :(得分:5)
最好将它投射到HttpContextBase
,这样你就可以更轻松地模拟和测试它
public string GetUserIp(HttpRequestMessage request)
{
if (request.Properties.ContainsKey("MS_HttpContext"))
{
var ctx = request.Properties["MS_HttpContext"] as HttpContextBase;
if (ctx != null)
{
return ctx.Request.UserHostAddress;
}
}
return null;
}
答案 5 :(得分:4)
我认为这是最明确的解决方案,使用扩展方法:
public static class HttpRequestMessageExtensions
{
private const string HttpContext = "MS_HttpContext";
private const string RemoteEndpointMessage = "System.ServiceModel.Channels.RemoteEndpointMessageProperty";
public static string GetClientIpAddress(this HttpRequestMessage request)
{
if (request.Properties.ContainsKey(HttpContext))
{
dynamic ctx = request.Properties[HttpContext];
if (ctx != null)
{
return ctx.Request.UserHostAddress;
}
}
if (request.Properties.ContainsKey(RemoteEndpointMessage))
{
dynamic remoteEndpoint = request.Properties[RemoteEndpointMessage];
if (remoteEndpoint != null)
{
return remoteEndpoint.Address;
}
}
return null;
}
}
所以就像使用它一样:
var ipAddress = request.GetClientIpAddress();
我们在项目中使用它。
答案 6 :(得分:3)
回复这篇4年前的帖子,因为这对我来说似乎过于复杂,至少如果你在IIS上主持这篇帖子对于“web api控制器获取IP地址”的DuckDuckGo(可能是NSAoogle)非常高。
以下是我如何解决它:
using System;
using System.Net;
using System.Web;
using System.Web.Http;
...
[HttpPost]
[Route("ContactForm")]
public IHttpActionResult PostContactForm([FromBody] ContactForm contactForm)
{
var hostname = HttpContext.Current.Request.UserHostAddress;
IPAddress ipAddress = IPAddress.Parse(hostname);
IPHostEntry ipHostEntry = Dns.GetHostEntry(ipAddress);
...
与OP不同,它为我提供了客户端IP和客户端主机名,而不是服务器。也许从那时起他们就修复了这个错误?
答案 7 :(得分:2)
我的解决方案类似于user1587439的答案,但直接在控制器的实例上工作(而不是访问HttpContext.Current)。
在观看'窗口,我看到this.RequestContext.WebRequest包含' UserHostAddress'属性,但由于它依赖于WebHostHttpRequestContext类型(它是System.Web.Http'程序集的内部) - 我无法直接访问它,所以我使用反射来直接访问它:
string hostAddress = ((System.Web.HttpRequestWrapper)this.RequestContext.GetType().Assembly.GetType("System.Web.Http.WebHost.WebHostHttpRequestContext").GetProperty("WebRequest").GetMethod.Invoke(this.RequestContext, null)).UserHostAddress;
我并不是说这是最好的解决方案。在框架升级的情况下(由于名称更改),使用反射可能会导致将来出现问题,但是对于我的需求,它是完美的
答案 8 :(得分:0)
string userRequest = System.Web.HttpContext.Current.Request.UserHostAddress;
这对我有效。
{System.Web.HttpContext.Current.Request.UserHostName;
,这一次我给我的回报与从UserHostAddress
中得到的回报一样。