From 6c20713d81a8716ddd064b4acd9497deca6fe5ba Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Sun, 27 Oct 2024 04:09:31 +0300 Subject: [PATCH] feat: add docker as localhost --- Endpoint/Common/Attributes/LocalhostAttribute.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/Endpoint/Common/Attributes/LocalhostAttribute.cs b/Endpoint/Common/Attributes/LocalhostAttribute.cs index 734e0bf..7a483ca 100644 --- a/Endpoint/Common/Attributes/LocalhostAttribute.cs +++ b/Endpoint/Common/Attributes/LocalhostAttribute.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; +using System; using System.Net; namespace Mirea.Api.Endpoint.Common.Attributes; @@ -9,11 +10,21 @@ public class LocalhostAttribute : ActionFilterAttribute public override void OnActionExecuting(ActionExecutingContext context) { var ip = context.HttpContext.Connection.RemoteIpAddress; - if (ip == null || !IPAddress.IsLoopback(ip)) + + if (ip == null) { context.Result = new UnauthorizedResult(); return; } - base.OnActionExecuting(context); + + var isRunningInContainer = Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER")?.ToLower() == "true"; + + if (IPAddress.IsLoopback(ip) || (isRunningInContainer && ip.ToString().StartsWith("172."))) + { + base.OnActionExecuting(context); + return; + } + + context.Result = new UnauthorizedResult(); } } \ No newline at end of file