Cube/NewLife.CubeNC/WebMiddleware/TenantMiddleware.cs

48 lines
1.3 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using XCode.Membership;
using HttpContext = Microsoft.AspNetCore.Http.HttpContext;
namespace NewLife.Cube.WebMiddleware;
/// <summary>租户中间件。设置租户上下文</summary>
public class TenantMiddleware
{
private readonly RequestDelegate _next;
/// <summary>实例化</summary>
/// <param name="next"></param>
public TenantMiddleware(RequestDelegate next) => _next = next ?? throw new ArgumentNullException(nameof(next));
/// <summary>调用</summary>
/// <param name="ctx"></param>
/// <returns></returns>
public async Task Invoke(HttpContext ctx)
{
// 找到租户并设置上下文。该上下文将全局影响魔方和XCode
var changed = false;
try
{
var set = CubeSetting.Current;
if (set.EnableTenant && TenantContext.Current == null)
{
var tenantId = ctx.GetTenantId();
if (tenantId > 0)
{
ctx.SetTenant(tenantId);
changed = true;
}
}
await _next.Invoke(ctx);
}
finally
{
if (changed)
{
TenantContext.Current = null;
ManageProvider.Provider.Tenant = null;
}
}
}
}