/
Frozin
/
DIS-3
Обзор
Документация
Войти
/
Frozin
/
DIS-3
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
API_PIR/Middleware/ApiKeyMiddleware.cs
41 строка
1 KB
Dobryk
Добавлен внутренний API
20 окт 2025, 05:26
20 окт 2025, 05:26
7ee684c
Код
Авторство
О чём код?
using API_PIR.Interfaces; namespace API_PIR.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 ключ"); return; } if (!_apiKeyService.ValidateApiKey(extractedApiKey!)) { context.Response.StatusCode = StatusCodes.Status401Unauthorized; await context.Response.WriteAsync("Неверный API ключ"); return; } await _next(context); } }