using Deal.Api.Extensions; using Deal.Api.Models; using Deal.Modules.Tenants.Application.Services; using Microsoft.Extensions.Options; // Имя конфигурационного типа совпадает с Microsoft.AspNetCore.Http.CookieOptions — фиксируем алиасом. using OperatorCookieOptions = Deal.Api.Configuration.OperatorCookieOptions; namespace Deal.Api.Middleware; /// /// Middleware операторской сессии /// public sealed class OperatorSessionMiddleware { private readonly RequestDelegate _next; private readonly IOptionsMonitor _cookieOptions; public OperatorSessionMiddleware(RequestDelegate next, IOptionsMonitor cookieOptions) { _next = next; _cookieOptions = cookieOptions; } /// /// Обрабатывает запрос /// public async Task InvokeAsync(HttpContext context) { string cookieName = _cookieOptions.CurrentValue.Name; if (context.Request.Cookies.TryGetValue(cookieName, out string? rawToken) && !string.IsNullOrWhiteSpace(rawToken)) { // OperatorAuthService scoped: создаём scope на запрос через RequestServices. await using var scope = context.RequestServices.CreateAsyncScope(); var operatorAuthService = scope.ServiceProvider.GetRequiredService(); var identity = await operatorAuthService.ResolveSessionAsync(rawToken, context.RequestAborted); if (identity is not null) { context.SetCurrentOperator(new CurrentOperator(identity.Id, identity.Login, identity.Status)); } } await _next(context); } }