2016-06-29 2 views
4

Ich habe versucht:Holen Sie die IP-Adresse des Clients zu einer .NET WebAPI Anwendung C# Verbindungs ​​

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

nach:

Retrieving the client's IP address in ASP.Net Web API

dies ist der kombinierte Ansatz, der für gültig sein sollte Selbst- und Webapi-Host. Leider bekomme ich statt der IP-Adresse null.

Ich versuche, lokal so ich 127.0.0.1 oder localhost als IP-Adresse erwarten würde

Antwort

10

Hier ist eine erweiterte Version von dem, was Sie haben, die für mich funktioniert.

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; 
     } 
     // Always return all zeroes for any 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; 
    } 

} 

Vorausgesetzt, dass Sie in einem Controller sind die oben Erweiterungsmethode wie für Anrufe erlaubt:

HttpRequestMessage request = this.Request; 

var ip = request.GetClientIpString(); 
+0

, was die "this" dreht sich alles um? Ich meine, auf dem nach dem Parameter –

+0

ist es eine Erweiterungsmethode. Wenn in einer statischen Klasse Aufrufe wie 'request.GetClientIpString() erlaubt sind;' – Nkosi

+0

bekomme ich :: 1 von meinem localhost webapi .. glaubst du, ist es korrekt? Ist es ein Alias ​​für 127.0.0.1? –

Verwandte Themen