Asp Net Web API 2.1 Gets Client IP Address

Hello, I need to get the IP address of a client that requests a method in the web api, I tried to use this code here , but it always returns the local IP address of the server, how to go?

HttpContext.Current.Request.UserHostAddress; 

from other questions:

 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; } } 
+105
c # asp.net-web-api
Mar 20 '14 at 12:19
source share
7 answers

The following link may help you. Here is the code from the following link.

link: 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; } } } } 

Another way to do it below.

link: how-to-access-the-client-s-ip-address

For web version

 string clientAddress = HttpContext.Current.Request.UserHostAddress; 

For self-placement

 object property; Request.Properties.TryGetValue(typeof(RemoteEndpointMessageProperty).FullName, out property); RemoteEndpointMessageProperty remoteProperty = property as RemoteEndpointMessageProperty; 
+113
Mar 20 '14 at 12:23
source share

With Web API 2.2: Request.GetOwinContext().Request.RemoteIpAddress

+69
Aug 07 '14 at 18:16
source share

Try using ip using

 ip = HttpContext.Current != null ? HttpContext.Current.Request.UserHostAddress : ""; 
+15
Mar 20 '14 at 17:48
source share

If you own your own hosting with Asp.Net 2.1, using the NuGet package for OWIN self-hosting, you can use the following code:

  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; } 
+9
Apr 10 '14 at 21:08
source share

It's better to drop it to an HttpContextBase , so you can more easily cheat and test it

 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
Jan 23 '15 at 13:02
source share

I think this is the clearest solution using the extension method:

 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; } } 

So just use it like:

 var ipAddress = request.GetClientIpAddress(); 

We use this in our projects.

Source / Link: Retrieving IP Addresses of Clients in ASP.NET Web API

+4
Mar 15 '17 at 15:45
source share

My solution is similar to the user1587439 answer, but works directly on the controller instance (instead of accessing HttpContext.Current).

In the Watch window, I saw that this.RequestContext.WebRequest contains the UserHostAddress property, but since it uses the WebHostHttpRequestContext type (which is internal to the "System.Web.Http" assembly) - I could not access it directly, so I used reflection for direct access to it:

string hostAddress = ((System.Web.HttpRequestWrapper)this.RequestContext.GetType().Assembly.GetType("System.Web.Http.WebHost.WebHostHttpRequestContext").GetProperty("WebRequest").GetMethod.Invoke(this.RequestContext, null)).UserHostAddress;

I do not think this is the best solution. using reflection may cause problems in the future if the framework is updated (due to name changes), but for my needs it is ideal

+2
Aug 07 '17 at 9:01 on
source share



All Articles