MireaBackend/Endpoint/Common/Attributes/LocalhostAttribute.cs

19 lines
543 B
C#
Raw Normal View History

2024-05-29 03:30:00 +03:00
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);
}
}