Holen Sie sich die IP-Adresse des Clients, der eine Verbindung zu einer C # .NET-WebAPI-Anwendung herstellt.
Ich habe es versucht
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;
}
gemäß
Abrufen der IP-Adresse des Clients in der ASP.Net-Web-API
this ist der kombinierte Ansatz, der für Self-Host und Webapi-Host gültig sein sollte. Leider bekomme ichnull
anstelle der IP-Adresse.
Ich versuche vor Ort, also würde ich erwarten127.0.0.1
oderlocalhost
als IP-Adresse