In classic ASP.NET we used to get client IP Address by Request.UserHostAddress
. But this does not apply to ASP.NET Core 2.0. We need a different way to retrieve HTTP Request information.
1. Define a variable in your MVC controller
private IHttpContextAccessor _accessor;
2. DI into the controller's constructor
public SomeController(IHttpContextAccessor accessor)
{
_accessor = accessor;
}
3. Retrive the IP Address
_accessor.HttpContext.Connection.RemoteIpAddress.ToString()
This is how it is done.
If your ASP.NET Core project is created with the default MVC template, you should have the DI for HttpContextAcccessor
in Startup.cs
public void ConfigureServices(IServiceCollection services)
{
...
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
...
}
2018/9/28 Update:
In ASP.NET 2.1, you can do this instead:
services.AddHttpContextAccessor();
services.TryAddSingleton<IActionContextAccessor, ActionContextAccessor>();
The RemoteIpAddress
is in type of IPAddress
, not string
. It contains IPv4, IPv6 and other information, it is not like the classic ASP.NET, which will be more useful to us.
If you want to access this information in Razor Views (cshtml). Just use @inject
to DI into the view:
@inject Microsoft.AspNetCore.Http.IHttpContextAccessor HttpContextAccessor
and then use it in your razor page:
Client IP: @HttpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString()
Chedy Missaoui
Thank you Edi for this helpful content, I have a question: Is it possible to get the subnet mask of the client's ip address?
Yaz
It returns "::1"
purushotham.s
How to get the Client machine name in asp.net core
vaindil
Is there a reason you can't just use HttpContext.Connection.RemoteIpAddress?
Plamen Ivanov
Why do you inject if you inherit Controller and can access HttpContext
Tony M
Thanks for the article. I'm not sure about when this was writting, but it's not necessary to use DI if you're accessing the context from a controller. If you're inside a controller in .NET Core 2.1, for example, you can just access it via HttpContext.Connection.RemoteIpAddress as it's a property on the base class.
mike
Thanks, worked for me.[ I had to add:
using Microsoft.AspNetCore.Mvc.Infrastructure; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions;
to my Startup.cs file, but i was using a blank asp.net core template.
Dmitry
You can use just Request.HttpContext.Connection.RemoteIpAddress from any type which has access to HttpRequest
Dmitry
You can use just Request.HttpContext.Connection.RemoteIpAddress from any type which has access to HttpRequest I use extension method to get client IP from Request even when he uses proxy
public static clas* **tensions { public static string GetIpAddress(this HttpRequest request) { var emptyValues = default(StringValues); StringValues header = request.Headers.FirstOrDefault(h => h.Key == "CF-Connecting-IP").Value; if (header == emptyValues) header = request.Headers.FirstOrDefault(h => h.Key == "X-Forwarded-For").Value;
if (header != emptyValues) return header.First(); else return request.HttpContext.Connection.RemoteIpAddress.ToString(); } }
Tim Smith
Many thanks, that's given me a quick working answer to something that's been bugging me for a couple of days
belleDic
Hello i am new user and i would to ask you, How to disable avatar?