using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; using System; using System.Net; namespace Mirea.Api.Endpoint.Common.Attributes; public class LocalhostAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext context) { var ip = context.HttpContext.Connection.RemoteIpAddress; if (ip == null) { context.Result = new UnauthorizedResult(); return; } 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(); } }