In ASP.NET 2.1, In StartUp.cs Add This Services:
services.AddHttpContextAccessor();
services.TryAddSingleton<IActionContextAccessor, ActionContextAccessor>();
and then do 3 step:
Define a variable in your MVC controller
private IHttpContextAccessor _accessor;
DI into the controller's constructor
public SomeController(IHttpContextAccessor accessor)
{
_accessor = accessor;
}
Retrieve the IP Address
_accessor.HttpContext.Connection.RemoteIpAddress.ToString()
This is how it is done.