2

I am getting IP address by following code:

var remoteIpAddress = HttpContext?.Connection?.RemoteIpAddress?.ToString ();

This is working okay when I am running on IIS with accessing by IP address (public IP address of my ISP network).

I host on another server but mapping to domain name (cannot be accessed by IP due to multiple website hosting on IIS).

It is working fine just not getting the remote IP address of client acceessing.

Also, I try the following code recommended by Microsoft but it is not working.

  services.Configure<ForwardedHeadersOptions>(options =>
            {
                options.ForwardedHeaders = 
                    ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
            });
S. Zahir
  • 11
  • 4
Sras
  • 1,686
  • 2
  • 19
  • 29

1 Answers1

0

Here is a solution to get client IP address in ASP.NET Core.

Inject the HttpContextAccessor instance in the ConfigureServices method from the Startup.cs class. Now add this reference using Microsoft.AspNetCore.Http.

public void ConfigureServices(IServiceCollection services)
    {
         services.AddControllersWithViews();
         services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
    }

Declare a variable.

private readonly IActionContextAccessor _accessor;

Update the HomeController to inject the IActionContextAccessor in the constructor. try to inject the IActionContextAccessor into the controller’s constructor with DI.

public HomeController(ILogger logger, IActionContextAccessor accessor)
{
    _logger = logger;
    _accessor = accessor;
}

Now at last step try to retrieve the IP address from the accessor variable.

public IActionResult Index()
{
  var ip = _accessor.ActionContext.HttpContext.Connection.RemoteIpAddress.ToString();
  return View();
}

If you don not want to use DI, you can always access the IP information directly in the controller as followed.

var ip = HttpContext.Connection.RemoteIpAddress.ToString();
S. Zahir
  • 11
  • 4
  • Yes this is what I’m doing but it is not getting the remote ip but router mikrotik ip instead – Sras Aug 10 '20 at 14:37
  • use Hairpin from following link [link](https://yaleman.org/2014/10/23/hairpin-nat-on-mikrotik-v6-19/) – S. Zahir Aug 10 '20 at 18:45
  • do you mean the Mikrotik is blocking/changing the remote ip address – Sras Aug 11 '20 at 14:19
  • yes, check the link which is added in my last comment. Read careful hopefully it will solve your problem. – S. Zahir Aug 11 '20 at 18:30
  • im not good at Mikrotic. do you have any video showing how to do this exactly – Sras Aug 12 '20 at 01:50
  • Please check this viideo. I hope it will help you. [Video Link](https://www.youtube.com/watch?v=_kw_bQyX-3U) – S. Zahir Aug 12 '20 at 03:04
  • I followed this videos. the configuration followed by video working, but the ip address is still getting the local ip address (192.168.1.1) which supposed to be remote public ip address – Sras Aug 12 '20 at 03:42
  • `services.AddHttpContextAccessor();` `services.TryAddSingleton();` 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()` – S. Zahir Aug 12 '20 at 06:43
  • im not sure what is wrong! but still not accessible – Sras Aug 12 '20 at 06:56
  • it is all about backend api! I get the ip address and store in database – Sras Aug 12 '20 at 06:57