Get the IP address of the client connecting to a C# .NET WebAPI application -


i tried:

private const string httpcontext = "ms_httpcontext"; private const string remoteendpointmessage = "system.servicemodel.channels.remoteendpointmessageproperty";  public static string getclientipaddress(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; } 

according to:

retrieving client's ip address in asp.net web api

this combined approach should valid self host , webapi host. unfortunately null instead of ip address.

i'm trying locally i'd expect 127.0.0.1 or localhost ip address

here expanded version of have works me.

static class httprequestmessageextensions {      private const string httpcontext = "ms_httpcontext";     private const string remoteendpointmessage = "system.servicemodel.channels.remoteendpointmessageproperty";     private const string owincontext = "ms_owincontext";      public static string getclientipstring(this httprequestmessage request) {         //web-hosting         if (request.properties.containskey(httpcontext)) {             dynamic ctx = request.properties[httpcontext];             if (ctx != null) {                 return ctx.request.userhostaddress;             }         }         //self-hosting         if (request.properties.containskey(remoteendpointmessage)) {             dynamic remoteendpoint = request.properties[remoteendpointmessage];             if (remoteendpoint != null) {                 return remoteendpoint.address;             }         }         //owin-hosting         if (request.properties.containskey(owincontext)) {             dynamic ctx = request.properties[owincontext];             if (ctx != null) {                 return ctx.request.remoteipaddress;             }         }         if (system.web.httpcontext.current != null) {             return system.web.httpcontext.current.request.userhostaddress;         }         // return zeroes failure         return "0.0.0.0";     }      public static ipaddress getclientipaddress(this httprequestmessage request) {         var ipstring = request.getclientipstring();         ipaddress ipaddress = new ipaddress(0);         if (ipaddress.tryparse(ipstring, out ipaddress)) {             return ipaddress;         }          return ipaddress;     }  } 

assuming in controller above extension method allows calls like:

httprequestmessage request = this.request;  var ip = request.getclientipstring(); 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -