How to get Client IP address in ASP.NET Core 2.1

前端 未结 7 678
再見小時候
再見小時候 2021-02-02 09:21

I\'m working on ASP.Net Core 2.1 with Angular Template provided by Microsoft Visual Studio 2017. My Client App is working fine. After competition of User Authentication, I want

相关标签:
7条回答
  • 2021-02-02 09:52

    If I use the Binding Address Localhost:5000 then the IP is returned as "::1" (Localhost IPv6 address). If I bind my Webapi on the IP Address and try to reach it from another client computer, I get Client's IP Address in API Response. There is no need for HTTPAccessor i believe. As per the documentation https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer?view=aspnetcore-2.1, the HttpContext.Connection.RemoteIpAddress is set by XForwardedFor header.

    If your application (WEBAPI) is behind a NGINX/Apache Reverse Proxy, you should enable those REV Proxies to send X-Forwarded-For Header address which contains the real IP address of the Client, if you don't setup or process X-Forwarded-For Header, then you would always get either Nulls or Reverse-Proxy Server's IP Address.

    The GetHostEntry above has no relation to the HTTP Request directly. GetHostEntry is just a NSLookup tool for API programming and it just tells you the IP Addresses reachable for a particular name, but doesn't tell you from which IP address the Request came to WebApi.

    Hope that helps

    0 讨论(0)
  • 2021-02-02 09:56

    After spending some time on searching I found my own question answer. Here I'm also sharing the source link from where I can get my answer and detail explanation for how to query a server to obtain the family addresses and the IP addresses it supports.

    Code:

    IPHostEntry heserver = Dns.GetHostEntry(Dns.GetHostName());
    var ip = heserver.AddressList[2].ToString();
    

    Source

    Here is my another Question: How to access server variables in ASP.Net Core 2.x Hope this helps for you all.

    0 讨论(0)
  • 2021-02-02 10:04

    This works for me on .Net Core 2.2:

    IPHostEntry heserver = Dns.GetHostEntry(Dns.GetHostName());
    
    var ipAddress = heserver.AddressList.ToList().Where(p => p.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).FirstOrDefault().ToString();
    
    0 讨论(0)
  • 2021-02-02 10:06

    It's possible to use the following code:

    services.Configure<ForwardedHeadersOptions>(options =>
    {
        options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
    });
    
    string remoteIpAddress = HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
    if (Request.Headers.ContainsKey("X-Forwarded-For"))
        remoteIpAddress = Request.Headers["X-Forwarded-For"];
    
    0 讨论(0)
  • 2021-02-02 10:09

    If your Kestrel sits behind a reverse proxy like IIS make sure to forward the headers containing the client IP.
    This goes into startup:

    app.UseForwardedHeaders(new ForwardedHeadersOptions{ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto});
    
    0 讨论(0)
  • 2021-02-02 10:17

    In your Startup.cs, make sure you have a method to ConfigureServices, passing in the IServiceCollection, then register IHttpContextAccessor as a singleton as follows:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
    }
    

    After registering the IHttpContextAccessor in your Startup.cs file, you can inject the IHttpContextAccessor in your controller class and use it like so:

    private IHttpContextAccessor _accessor;
    
    public ValuesController(IHttpContextAccessor accessor)
    {
        _accessor = accessor;
    }
    
    public IEnumerable<string> Get()
    {
        var ip = _accessor.HttpContext?.Connection?.RemoteIpAddress?.ToString();
        return new string[] { ip, "value2" };
    }
    
    0 讨论(0)
提交回复
热议问题