Asp Net Web API 2.1 get adres IP klienta
Witam potrzebuję uzyskać adres IP klienta, który żąda jakiejś metody w web api, Próbowałem użyć tego kodu z TUTAJ ale zawsze zwraca lokalny adres IP serwera, jak wejść w poprawny sposób ?
HttpContext.Current.Request.UserHostAddress;
Z innych pytań:
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;
}
}
9 answers
Poniższy link może Ci pomóc. Oto kod z poniższego linku.
W związku z tym, że nie jest to możliwe, nie jest to możliwe.]}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;
}
}
}
}
Inny sposób na to jest poniżej.
Reference: how-to-access-the-client-s-ip-address
Dla wersji web hosted
string clientAddress = HttpContext.Current.Request.UserHostAddress;
Do samodzielnego hostingu
object property;
Request.Properties.TryGetValue(typeof(RemoteEndpointMessageProperty).FullName, out property);
RemoteEndpointMessageProperty remoteProperty = property as RemoteEndpointMessageProperty;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-01-02 09:02:09
Z Web API 2.2: Request.GetOwinContext().Request.RemoteIpAddress
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-08-07 18:16:13
Spróbuj uzyskać Ip używając
ip = HttpContext.Current != null ? HttpContext.Current.Request.UserHostAddress : "";
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-03-20 17:48:24
Jeśli sam hostujesz z Asp.Net 2.1 używając pakietu OWIN Self-host NuGet możesz użyć następującego kodu:
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;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-10 21:08:41
Odpowiadam na ten 4-letni post, ponieważ wydaje mi się to zbyt skomplikowane, przynajmniej jeśli hostujesz na IIS.
Oto Jak to rozwiązałem:
using System;
using System.Net;
using System.Web;
using System.Web.Http;
...
[HttpPost]
[Route("ContactForm")]
public IHttpActionResult PostContactForm([FromBody] ContactForm contactForm)
{
var hostname = HttpContext.Current.Request.UserHostAddress;
IPAddress ipAddress = IPAddress.Parse(hostname);
IPHostEntry ipHostEntry = Dns.GetHostEntry(ipAddress);
...
W przeciwieństwie do OP, to daje mi adres IP klienta i nazwę hosta klienta, a nie serwer. Może od tego czasu naprawili błąd?
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-10-07 07:46:21
Myślę, że jest to najbardziej jasne rozwiązanie, używając metody rozszerzenia:
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;
}
}
Więc używaj go tak:
var ipAddress = request.GetClientIpAddress();
Wykorzystujemy to w naszych projektach.
Source/Reference: Odzyskiwanie adresu IP klienta w ASP.NET Web API
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-15 15:45:56
Lepiej rzucić go do HttpContextBase
, w ten sposób można wyśmiewać i testować łatwiej
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;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-09-20 07:59:32
Moje rozwiązanie jest podobne do odpowiedzi user1587439, ale działa bezpośrednio na instancji kontrolera (zamiast uzyskiwania dostępu do HttpContext.Prąd).
W oknie "Zegarek", widziałem, że to.RequestContext.WebRequest zawiera właściwość 'UserHostAddress', ale ponieważ opiera się na typie WebHostHttpRequestContext (który jest wewnętrzny do systemu'.Www.Http ' assembly) - nie byłem w stanie uzyskać do niego bezpośredniego dostępu, więc użyłem reflection, aby uzyskać do niego bezpośredni dostęp:
string hostAddress = ((System.Web.HttpRequestWrapper)this.RequestContext.GetType().Assembly.GetType("System.Web.Http.WebHost.WebHostHttpRequestContext").GetProperty("WebRequest").GetMethod.Invoke(this.RequestContext, null)).UserHostAddress;
Nie jestem mówiąc, że to najlepsze rozwiązanie. używanie reflection może powodować problemy w przyszłości w przypadku aktualizacji frameworka (ze względu na zmiany nazw), ale dla moich potrzeb jest to idealne rozwiązanie
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-07 09:01:17
string userRequest = System.Web.HttpContext.Current.Request.UserHostAddress;
To działa na mnie.
System.Web.HttpContext.Current.Request.UserHostName;
ten zwraca mi ten sam zwrot, który otrzymuję z UserHostAddress
.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-09-24 21:44:46