反向代理后面的ASP.Net Core不使用“ X-Forwareded-For”

时间:2018-09-30 11:02:16

标签: c# asp.net-core .net-core

我试图在我的 ASP.Net Core 2.1 MVC中获取请求的远程IP地址(即,发送请求的客户端的IP)。控制器(在.Net Docker容器中运行)。考虑到我的ASP.Net Core应用程序位于NGINX反向代理后面(在NGINX Docker容器中运行)。

众所周知,当反向代理将请求重定向到我的.Net Core应用程序时,它将更改我的请求的源IP(TCP / IP层),因此,我将NGINX配置为将X-Forwareded-For添加到请求的原始IP。从NGINX容器重定向到.Net容器的请求的标头中有X-Forwareded-For

enter image description here

当然,我配置了.Net Core来了解这一点:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider)
{
    // Rewrite the header when being redirected (this is required because we are behind reverse proxy)
    var forwardedHeadersOptions = new ForwardedHeadersOptions
    {
        ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto,
        KnownProxies = { IPAddress.Parse("172.20.10.2"), IPAddress.Parse("172.20.10.3"), IPAddress.Parse("172.20.10.4") },
    };

    forwardedHeadersOptions.KnownNetworks.Add(
        new IPNetwork(IPAddress.Parse("172.0.0.0"), 8));
    forwardedHeadersOptions.KnownNetworks.Add(
        new IPNetwork(IPAddress.Parse("127.0.0.1"), 8));

    app.UseForwardedHeaders(forwardedHeadersOptions);
    ...

但是,HTTPContext.Connection.RemoteIPAddress仍返回172.20.10.3(NGINX容器IP,而不是真正的远程IP):

logger.LogDebug("Remote IP Address: " + Request.HttpContext.Connection.RemoteIpAddress.ToString());
  

远程IP地址:172.20.10.3

我在.Net Core中检查了标头,其中的X-Forwareded-For具有正确的原始远程IP地址:

logger.LogDebug("X-Forwareded-For Header Feature: " + HttpContext.Request.Headers["X-Forwarded-For"]);
  

X-Forwareded-For标头功能:85.XX.4.121

有人知道我在想什么吗?为什么RemoteIPAddress仍返回NGINX docker容器的IP而不是真实的远程IP地址?

更新

我的Program.cs

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseUrls("http://*:5000")
            .UseStartup<Startup>()
            .Build();
}

我还尝试过通过配置ForwarededHeadersOptions的服务来配置services.Configure<ForwardedHeadersOptions>(options => { options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto; //options.KnownNetworks.Add(new IPNetwork(IPAddress.Parse("172.0.0.0"), 8)); options.RequireHeaderSymmetry = false; options.ForwardLimit = null; options.KnownProxies.Add(IPAddress.Parse("172.20.10.3")); //options.KnownNetworks.Add(new IPNetwork(IPAddress.Parse("127.0.0.1"), 8)); });

RemoteIPAddress

没有成功...

更新2

好的,我想我是对的,// convert $timestamps string to array $timestamps_arr = explode(',', $timestamps); $datetimestring_arr = array(); // initialize array for datetime strings // Loop over the array to convert into datetime string foreach ($timestamps_arr as $timestamp) { // convert the timestamp to datetime string $datetimestring_arr[] = date('Y/m/d H:i:s', $timestamp); } // convert it back to comma separated string $output = implode(',', $datetimestring_arr); // display the output echo $output; 返回的IP地址是:: ffff:172.20.10.20,而不是172.20.10.20!我不知道他们是不同的。 official documentation帮助我发现了这一点。

1 个答案:

答案 0 :(得分:2)

我从RemoteIPAddress获得的IP是IPv4,表示为IPv6(是:: ffff:172.20.10.20,而不是172.20.10.20!)。我仅使用IP的IPv4部分,因此我的KnownProxies是错误的。我还应该在v6表示部分中输入完整的IPv4地址。

官方文件显示:

https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer?view=aspnetcore-2.1