Гейт csharp_style_var_for_built_in_types=false:warning (ломает сборку), остаток выправлен dotnet format IDE0008 по 5 sln (только встроенные типы). Понижено 12 новых XML-доков private/internal, добавлены <summary> членам IContainerRules и ITenantContext, 3 англоязычных комментария переведены на русский.
46 lines
1.8 KiB
C#
46 lines
1.8 KiB
C#
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;
|
|
|
|
/// <summary>
|
|
/// Middleware операторской сессии
|
|
/// </summary>
|
|
public sealed class OperatorSessionMiddleware
|
|
{
|
|
private readonly RequestDelegate _next;
|
|
private readonly IOptionsMonitor<OperatorCookieOptions> _cookieOptions;
|
|
|
|
public OperatorSessionMiddleware(RequestDelegate next, IOptionsMonitor<OperatorCookieOptions> cookieOptions)
|
|
{
|
|
_next = next;
|
|
_cookieOptions = cookieOptions;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Обрабатывает запрос
|
|
/// </summary>
|
|
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<OperatorAuthService>();
|
|
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);
|
|
}
|
|
}
|