From 9de290aca6e9ff142623ba5831369198b0c7604d Mon Sep 17 00:00:00 2001 From: Mikhail Trubnikov Date: Mon, 4 Dec 2023 10:56:29 +1000 Subject: [PATCH] =?UTF-8?q?fix(api):=20=D0=BF=D0=BE=D0=BB=D1=83=D1=87?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F=20ip=20=D0=B0=D0=B4=D1=80=D0=B5=D1=81?= =?UTF-8?q?=D0=B0=20=D0=B7=D0=B0=20=D0=B1=D0=B0=D0=BB=D0=B0=D0=BD=D1=81?= =?UTF-8?q?=D0=B8=D1=80=D0=BE=D0=B2=D1=89=D0=B8=D0=BA=D0=BE=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PARR.API/Authentication/ParrAuthenticationHandler.cs | 6 +++--- PARR.API/Program.cs | 10 ++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/PARR.API/Authentication/ParrAuthenticationHandler.cs b/PARR.API/Authentication/ParrAuthenticationHandler.cs index 6574e7fc..bdd960f2 100644 --- a/PARR.API/Authentication/ParrAuthenticationHandler.cs +++ b/PARR.API/Authentication/ParrAuthenticationHandler.cs @@ -25,7 +25,7 @@ namespace PARR.API.Authentication var ipClient = Request.HttpContext.Connection.RemoteIpAddress?.MapToIPv4().ToString(); if (ipClient == null) - return AuthenticateResult.Fail($"IP address not defined."); + return AuthenticateResult.Fail($"IP address not defined. IP: {ipClient}"); // Аутентификация - просто проверка, есть ли у нас такой пользователь using (var scope = serviceProvider.CreateScope()) @@ -34,11 +34,11 @@ namespace PARR.API.Authentication var userIsBlocked = await authService.UserIsBlockedAsync(ipClient); if (userIsBlocked) - return AuthenticateResult.Fail($"IP address is on the blocking list."); + return AuthenticateResult.Fail($"IP address is on the blocking list. IP: {ipClient}"); var user = await authService.GetUserAsync(ipClient); if (user == null) - return AuthenticateResult.Fail($"IP address not defined."); + return AuthenticateResult.Fail($"IP address not defined. IP: {ipClient}"); // пользователь найден, аутентификация пройдена diff --git a/PARR.API/Program.cs b/PARR.API/Program.cs index d18a05e3..01e0232e 100644 --- a/PARR.API/Program.cs +++ b/PARR.API/Program.cs @@ -1,5 +1,6 @@ using Elastic.CommonSchema.Serilog; using FluentValidation; +using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.Server.HttpSys; using PARR.API.Authentication; using PARR.API.Installers; @@ -38,6 +39,15 @@ builder.Services.AddAuthentication(ParrAuthenticationOptions.DefaultScheme) // не используем, так как проверка ролей остается прежней, а роли подставляем в ParrAuthenticationHandler //builder.Services.AddSimpleRoleAuthorization(); +// При использовании балансировщика (haproxy, перенаправлять заголовки) +builder.Services.Configure(options => +{ + options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto; + + options.KnownNetworks.Clear(); + options.KnownProxies.Clear(); +}); + builder.Services.AddHttpContextAccessor();