37 lines
778 B
C#
37 lines
778 B
C#
using System.Security.Claims;
|
|
|
|
namespace line_gestao_api.Services;
|
|
|
|
public class TenantMiddleware
|
|
{
|
|
private readonly RequestDelegate _next;
|
|
|
|
public TenantMiddleware(RequestDelegate next)
|
|
{
|
|
_next = next;
|
|
}
|
|
|
|
public async Task InvokeAsync(HttpContext context, ITenantProvider tenantProvider)
|
|
{
|
|
Guid? tenantId = null;
|
|
var claim = context.User.FindFirst("tenantId")?.Value
|
|
?? context.User.FindFirst("tenant")?.Value;
|
|
|
|
if (Guid.TryParse(claim, out var parsed))
|
|
{
|
|
tenantId = parsed;
|
|
}
|
|
|
|
tenantProvider.SetTenantId(tenantId);
|
|
|
|
try
|
|
{
|
|
await _next(context);
|
|
}
|
|
finally
|
|
{
|
|
tenantProvider.SetTenantId(null);
|
|
}
|
|
}
|
|
}
|