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

前端 未结 7 677
再見小時候
再見小時候 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 10:06

    It's possible to use the following code:

    services.Configure(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"];
    

提交回复
热议问题