/
Lexus_666
/
PIR_Lab_3
Обзор
Документация
Войти
/
Lexus_666
/
PIR_Lab_3
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
PIR_Lab_1/Middleware/ApiKeyMiddleware.cs
41 строка
1 KB
MikhailPshenisnov
Добавлено внутреннее API
20 окт 2025, 04:49
20 окт 2025, 04:49
0815520
Код
Авторство
О чём код?
using PIR_Lab_1.Interfaces; namespace PIR_Lab_1.Middleware; public class ApiKeyMiddleware { private readonly RequestDelegate _next; private readonly IApiKeyService _apiKeyService; public ApiKeyMiddleware(RequestDelegate next, IApiKeyService apiKeyService) { _next = next; _apiKeyService = apiKeyService; } public async Task InvokeAsync(HttpContext context) { if (context.Request.Path.StartsWithSegments("/swagger") || context.Request.Path.StartsWithSegments("/api/internal")) { await _next(context); return; } if (!context.Request.Headers.TryGetValue("X-API-Key", out var extractedApiKey)) { context.Response.StatusCode = StatusCodes.Status401Unauthorized; await context.Response.WriteAsync("API Key is missing"); return; } if (!_apiKeyService.ValidateApiKey(extractedApiKey!)) { context.Response.StatusCode = StatusCodes.Status401Unauthorized; await context.Response.WriteAsync("Invalid API Key"); return; } await _next(context); } }