feat: add token for setup controllers
This commit is contained in:
parent
35eb1eab39
commit
fb6e119a34
27
Endpoint/Common/Attributes/TokenAuthenticationAttribute.cs
Normal file
27
Endpoint/Common/Attributes/TokenAuthenticationAttribute.cs
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.Mvc.Filters;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Mirea.Api.Endpoint.Common.Interfaces;
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Mirea.Api.Endpoint.Common.Attributes;
|
||||||
|
|
||||||
|
[AttributeUsage(AttributeTargets.Method)]
|
||||||
|
public class TokenAuthenticationAttribute : Attribute, IActionFilter
|
||||||
|
{
|
||||||
|
public void OnActionExecuting(ActionExecutingContext context)
|
||||||
|
{
|
||||||
|
var setupToken = context.HttpContext.RequestServices.GetRequiredService<ISetupToken>();
|
||||||
|
if (!context.HttpContext.Request.Cookies.TryGetValue("AuthToken", out string? tokenFromCookie))
|
||||||
|
{
|
||||||
|
context.Result = new UnauthorizedResult();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (setupToken.MatchToken(Convert.FromBase64String(tokenFromCookie))) return;
|
||||||
|
|
||||||
|
context.Result = new UnauthorizedResult();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnActionExecuted(ActionExecutedContext context) { }
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user