From 22793c78826a86779230f4eafcad97e8fb2eb987 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Wed, 29 May 2024 03:30:00 +0300 Subject: [PATCH] feat: add localhost attribute --- .../Common/Attributes/LocalhostAttribute.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Endpoint/Common/Attributes/LocalhostAttribute.cs diff --git a/Endpoint/Common/Attributes/LocalhostAttribute.cs b/Endpoint/Common/Attributes/LocalhostAttribute.cs new file mode 100644 index 0000000..734e0bf --- /dev/null +++ b/Endpoint/Common/Attributes/LocalhostAttribute.cs @@ -0,0 +1,19 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Filters; +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 || !IPAddress.IsLoopback(ip)) + { + context.Result = new UnauthorizedResult(); + return; + } + base.OnActionExecuting(context); + } +} \ No newline at end of file