Получить IP-адрес клиента, подключающегося к приложению C # .NET WebAPI
Я старался:
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;
}
в соответствии с:
Получение IP-адреса клиента в ASP.Net Web API
это комбинированный подход, который должен быть действительным для собственного хоста и хоста webapi. К сожалению я получаюnull
вместо IP-адреса.
Я пытаюсь локально, поэтому я ожидал127.0.0.1
или жеlocalhost
как IP-адрес