fix(api): получения ip адреса за балансировщиком
This commit is contained in:
@@ -25,7 +25,7 @@ namespace PARR.API.Authentication
|
|||||||
var ipClient = Request.HttpContext.Connection.RemoteIpAddress?.MapToIPv4().ToString();
|
var ipClient = Request.HttpContext.Connection.RemoteIpAddress?.MapToIPv4().ToString();
|
||||||
|
|
||||||
if (ipClient == null)
|
if (ipClient == null)
|
||||||
return AuthenticateResult.Fail($"IP address not defined.");
|
return AuthenticateResult.Fail($"IP address not defined. IP: {ipClient}");
|
||||||
|
|
||||||
// Аутентификация - просто проверка, есть ли у нас такой пользователь
|
// Аутентификация - просто проверка, есть ли у нас такой пользователь
|
||||||
using (var scope = serviceProvider.CreateScope())
|
using (var scope = serviceProvider.CreateScope())
|
||||||
@@ -34,11 +34,11 @@ namespace PARR.API.Authentication
|
|||||||
|
|
||||||
var userIsBlocked = await authService.UserIsBlockedAsync(ipClient);
|
var userIsBlocked = await authService.UserIsBlockedAsync(ipClient);
|
||||||
if (userIsBlocked)
|
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);
|
var user = await authService.GetUserAsync(ipClient);
|
||||||
if (user == null)
|
if (user == null)
|
||||||
return AuthenticateResult.Fail($"IP address not defined.");
|
return AuthenticateResult.Fail($"IP address not defined. IP: {ipClient}");
|
||||||
|
|
||||||
|
|
||||||
// пользователь найден, аутентификация пройдена
|
// пользователь найден, аутентификация пройдена
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using Elastic.CommonSchema.Serilog;
|
using Elastic.CommonSchema.Serilog;
|
||||||
using FluentValidation;
|
using FluentValidation;
|
||||||
|
using Microsoft.AspNetCore.HttpOverrides;
|
||||||
using Microsoft.AspNetCore.Server.HttpSys;
|
using Microsoft.AspNetCore.Server.HttpSys;
|
||||||
using PARR.API.Authentication;
|
using PARR.API.Authentication;
|
||||||
using PARR.API.Installers;
|
using PARR.API.Installers;
|
||||||
@@ -38,6 +39,15 @@ builder.Services.AddAuthentication(ParrAuthenticationOptions.DefaultScheme)
|
|||||||
// не используем, так как проверка ролей остается прежней, а роли подставляем в ParrAuthenticationHandler
|
// не используем, так как проверка ролей остается прежней, а роли подставляем в ParrAuthenticationHandler
|
||||||
//builder.Services.AddSimpleRoleAuthorization<BaseSimpleRoleProvider>();
|
//builder.Services.AddSimpleRoleAuthorization<BaseSimpleRoleProvider>();
|
||||||
|
|
||||||
|
// При использовании балансировщика (haproxy, перенаправлять заголовки)
|
||||||
|
builder.Services.Configure<ForwardedHeadersOptions>(options =>
|
||||||
|
{
|
||||||
|
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
|
||||||
|
|
||||||
|
options.KnownNetworks.Clear();
|
||||||
|
options.KnownProxies.Clear();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
builder.Services.AddHttpContextAccessor();
|
builder.Services.AddHttpContextAccessor();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user