Compare commits
3 Commits
35eb1eab39
...
481839159c
Author | SHA1 | Date | |
---|---|---|---|
481839159c | |||
59785f600f | |||
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) { }
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Mirea.Api.Endpoint.Common.Exceptions;
|
||||||
|
|
||||||
|
public class ControllerArgumentException(string message) : Exception(message);
|
60
Endpoint/Middleware/CustomExceptionHandlerMiddleware.cs
Normal file
60
Endpoint/Middleware/CustomExceptionHandlerMiddleware.cs
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
using FluentValidation;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Mirea.Api.DataAccess.Application.Common.Exceptions;
|
||||||
|
using Mirea.Api.Dto.Responses;
|
||||||
|
using Mirea.Api.Endpoint.Common.Exceptions;
|
||||||
|
using System;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Mirea.Api.Endpoint.Middleware;
|
||||||
|
|
||||||
|
public class CustomExceptionHandlerMiddleware(RequestDelegate next)
|
||||||
|
{
|
||||||
|
public async Task Invoke(HttpContext context)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await next(context);
|
||||||
|
}
|
||||||
|
catch (Exception exception)
|
||||||
|
{
|
||||||
|
await HandleExceptionAsync(context, exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Task HandleExceptionAsync(HttpContext context, Exception exception)
|
||||||
|
{
|
||||||
|
var code = StatusCodes.Status500InternalServerError;
|
||||||
|
var result = string.Empty;
|
||||||
|
switch (exception)
|
||||||
|
{
|
||||||
|
case ValidationException validationException:
|
||||||
|
code = StatusCodes.Status400BadRequest;
|
||||||
|
result = JsonSerializer.Serialize(new ErrorResponse()
|
||||||
|
{
|
||||||
|
Error = validationException.Message,
|
||||||
|
Code = code
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
case NotFoundException:
|
||||||
|
code = StatusCodes.Status404NotFound;
|
||||||
|
break;
|
||||||
|
case ControllerArgumentException:
|
||||||
|
code = StatusCodes.Status400BadRequest;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
context.Response.ContentType = "application/json";
|
||||||
|
context.Response.StatusCode = code;
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(result))
|
||||||
|
result = JsonSerializer.Serialize(new ErrorResponse()
|
||||||
|
{
|
||||||
|
Error = exception.Message,
|
||||||
|
Code = code
|
||||||
|
});
|
||||||
|
|
||||||
|
return context.Response.WriteAsync(result);
|
||||||
|
}
|
||||||
|
}
|
@ -138,6 +138,7 @@ public class Program
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
app.UseMiddleware<MaintenanceModeMiddleware>();
|
app.UseMiddleware<MaintenanceModeMiddleware>();
|
||||||
|
app.UseMiddleware<CustomExceptionHandlerMiddleware>();
|
||||||
|
|
||||||
app.UseHttpsRedirection();
|
app.UseHttpsRedirection();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user