ASP.NET Core 中使用负载均衡时获取客户端 IP

在使用负载均衡的情况下,通过 context.Connection.RemoteIpAddress 获取到的是负载均衡的 IP 地址,需要通过 X-Forwarded-For 请求头才能获取到客户端的真实 IP 。

之前采用的方式是自己直接获取 X-Forwarded-For 请求头,代码如下:

public static class HttpContextExtensions
{
    public static string GetUserIp(this HttpContext context)
    {
        var ip = context.Request.Headers["X-Forwarded-For"].FirstOrDefault();
        if (string.IsNullOrEmpty(ip))
        {
            ip = context.Connection.RemoteIpAddress?.ToString();
        }
        return GetSingleIP(ip);
    }

    private static string GetSingleIP(string ip)
    {
        if (!string.IsNullOrEmpty(ip))
        {
            var commaIndex = ip.LastIndexOf(",");
            if (commaIndex >= 0)
            {
                ip = ip.Substring(commaIndex + 1);
            }
        }
        return ip;
    }
}

现在改用 asp.net core 内置的 Forwarded Headers Middleware 来实现。

先在 Startup.ConfigureServices 中配置 ForwardedHeadersOptions ,对于获取客户端 IP 的场景只需要指定 ForwardedHeaders.XForwardedFor 。

services.Configure<ForwardedHeadersOptions>(options =>
{
    options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
    options.KnownNetworks.Clear();
    options.KnownProxies.Clear();
});

注1:如果负载均衡不是在本机通过 Loopback 地址转发请求的,一定要加上 options.KnownNetworks.Clear 与 options.KnownProxies.Clear 。

注2:如果设置环境变量 ASPNETCORE_FORWARDEDHEADERS_ENABLED 为 true ,就不需要上面的配置代码。

然后在 Startup.Configure 中添加 Forwarded Headers 中间件。

app.UseForwardedHeaders()

这样就可以通过 RemoteIpAddress 获取客户端的真实  IP 地址了。

var ip = Request.HttpContext.Connection.RemoteIpAddress?.MapToIPv4().ToString();
posted @ 2019-06-26 12:09  dudu  阅读(2252)  评论(5编辑  收藏  举报