59 lines
1.8 KiB
C#
59 lines
1.8 KiB
C#
using HuanMeng.DotNetCore.MultiTenant;
|
|
using HuanMeng.DotNetCore.MultiTenant.Contract;
|
|
using HuanMeng.MiaoYu.Code.AppExtend;
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace HuanMeng.MiaoYu.Code.MultiTenantUtil
|
|
{
|
|
/// <summary>
|
|
/// 多租户中间件
|
|
/// </summary>
|
|
public class MiaoYuMultiTenantTenantMiddleware
|
|
{
|
|
private readonly RequestDelegate _next;
|
|
public MiaoYuMultiTenantTenantMiddleware(RequestDelegate next)
|
|
{
|
|
_next = next;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 根据HttpContext获取并设置当前租户ID
|
|
/// </summary>
|
|
/// <param name="context"></param>
|
|
/// <param name="_serviceProvider"></param>
|
|
/// <param name="tenantInfo"></param>
|
|
/// <returns></returns>
|
|
public virtual async Task Invoke(HttpContext context,
|
|
IServiceProvider _serviceProvider,
|
|
ITenantInfo tenantInfo,
|
|
AppConfig appConfig
|
|
)
|
|
{
|
|
if (tenantInfo == null)
|
|
{
|
|
tenantInfo = new TenantInfo();
|
|
}
|
|
var host = context.Request.Host.Host;
|
|
var app = AppConfigurationExtend.GetAppConfig(host);
|
|
app.ToAppConfig(appConfig).ToITenantInfo(tenantInfo);
|
|
//var _ten = miaoYuMultiTenantConfig.GetMultiTenantCfgDefault();
|
|
//tenantInfo.ConnectionString = _ten.ConnectionString;
|
|
//tenantInfo.Identifier = _ten.Identifier;
|
|
//tenantInfo.TenantId = _ten.TenantId;
|
|
//tenantInfo.Name = _ten.Name;
|
|
await _next.Invoke(context);
|
|
}
|
|
}
|
|
}
|