What is the equivalent in ASP.NET Core of ASP.NET Framework's HttpContext.Request.UserHostAddress?
I tried this.ActionContext.HttpContext but cannot find the UserHostAddress nor the ServerVariables properties.
What is the equivalent in ASP.NET Core of ASP.NET Framework's HttpContext.Request.UserHostAddress?
I tried this.ActionContext.HttpContext but cannot find the UserHostAddress nor the ServerVariables properties.
This has moved since Badrinarayanan's answer in 2014 was posted. Access it now via
httpContext.Features.Get<IHttpConnectionFeature>()?.RemoteIpAddress
null for me. (I deploy the website on IIS on a VM)HttpRequest.UserHostAddress gives the IP address of the remote client. In ASP.NET Core 1.0, you have to use the HTTP connection feature to get the same. HttpContext has the GetFeature<T> method that you can use to get a specific feature. As an example, if you want to retrieve the remote IP address from a controller action method, you can do something like this.
var connectionFeature = Context
.GetFeature<Microsoft.AspNet.HttpFeature.IHttpConnectionFeature>();
if (connectionFeature != null)
{
string ip = connectionFeature.RemoteIpAddress.ToString();
}
For ASP.NET Core RC1-update1 I found IP (with port) in X-Forwarded-For header, whose value can be accessed from a controller as:
HttpContext.Request.Headers["X-Forwarded-For"].FirstOrDefault()