INIT vol 1.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user