37 lines
1.1 KiB
C#
37 lines
1.1 KiB
C#
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);
|
|
}
|
|
}
|
|
|
|
}
|