INIT vol 1.

This commit is contained in:
2024-12-17 19:23:15 +01:00
parent e201f8e89c
commit 8bbcbfab09
11 changed files with 206 additions and 23 deletions
+36
View File
@@ -0,0 +1,36 @@
namespace WorkFlowCheck.API
{
public class ApiKeyMiddleware
{
private readonly RequestDelegate _next;
private const string ApiKeyHeaderName = "X-Api-Key";
public ApiKeyMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
if (!context.Request.Headers.TryGetValue(ApiKeyHeaderName, out var extractedApiKey))
{
context.Response.StatusCode = 401; // Unauthorized
await context.Response.WriteAsync("API Key hiányzik.");
return;
}
var appSettings = context.RequestServices.GetRequiredService<IConfiguration>();
var apiKey = appSettings.GetValue<string>("ApiKey");
if (!apiKey.Equals(extractedApiKey))
{
context.Response.StatusCode = 403; // Forbidden
await context.Response.WriteAsync("Helytelen API Key.");
return;
}
await _next(context);
}
}
}