What is the equivalent in Asp.Net Core of the old HttpContext.Request.UserHostAddress
?
I tried this.ActionContext.HttpContext
but cannot find the UserHostAddress nor the ServerVariables properties.
Thanks
Client IP address can be retrieved via HttpContext. Connection object. This properties exist in both Razor page model and ASP.NET MVC controller. Property RemoteIpAddress is the client IP address.
It stores the request and response information, such as the properties of request, request-related services, and any data to/from the request or errors, if there are any. ASP.NET Core applications access the HTTPContext through the IHttpContextAccessor interface.
If you have access to the HttpContext
, you can get the Local/Remote IpAddress from the Connection
property like so:
var remote = this.HttpContext.Connection.RemoteIpAddress; var local = this.HttpContext.Connection.LocalIpAddress;
This has moved since the original answer was posted. Access it now via
httpContext.Features.Get<IHttpConnectionFeature>()?.RemoteIpAddress
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With