114 lines
4.1 KiB
C#
114 lines
4.1 KiB
C#
using System;
|
|
using System.Configuration;
|
|
using System.Threading.Tasks;
|
|
using APT.Infrastructure.Api;
|
|
using APT.Infrastructure.Api.Redis;
|
|
using APT.Infrastructure.Core;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using ConfigurationManager = APT.Infrastructure.Api.ConfigurationManager;
|
|
|
|
namespace APT.Infrastructure.EF
|
|
{
|
|
public class TenantInfoMiddleware
|
|
{
|
|
private readonly RequestDelegate _next;
|
|
//private string TenantConnKey = "TenantConnKey_";
|
|
public TenantInfoMiddleware(RequestDelegate next)
|
|
{
|
|
_next = next;
|
|
}
|
|
|
|
public async Task InvokeAsync(Microsoft.AspNetCore.Http.HttpContext context)
|
|
{
|
|
|
|
var tenantInfo = context.RequestServices.GetRequiredService<TenantInfo>();
|
|
var tenantName = context.Request.Headers["Tenant"];
|
|
APT.Infrastructure.Api.AppContext.CurrentSession.RootOrgId = null;
|
|
|
|
if (string.IsNullOrEmpty(tenantName) || tenantName == "null")
|
|
{
|
|
tenantInfo.Name = tenantName;
|
|
tenantInfo.Conn = ConfigurationManager.ConnectionStrings["default"];
|
|
}
|
|
else
|
|
{
|
|
if (tenantInfo.Name != tenantName)
|
|
{
|
|
tenantInfo.Conn = GetDbConn(tenantName);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
await _next(context);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据租户获取数据库连接
|
|
/// </summary>
|
|
/// <param name="tenantName"></param>
|
|
/// <returns></returns>
|
|
public static string GetDbConn(string tenantName)
|
|
{
|
|
//redis取數據
|
|
var deskey = APT.Infrastructure.Api.ConfigurationManager.AppSettings["ConnDataKey"];
|
|
var tenantConnKey = APT.Infrastructure.Api.ConfigurationManager.AppSettings["TenantConnKey"];
|
|
var env = ConfigurationManager.AppSettings["Env"];
|
|
if (string.IsNullOrEmpty(deskey))
|
|
deskey = "optenergy2021001";
|
|
if (string.IsNullOrEmpty(tenantConnKey))
|
|
tenantConnKey = "optiems";
|
|
var tenantKey = tenantConnKey + tenantName + "_" + env;
|
|
if (CsRedisManager.KeyExists(tenantKey))
|
|
{
|
|
var conns = CsRedisManager.StringGet(tenantKey);
|
|
var retConn = conns == "null" ? null : EncryptHelper.AesDecrypt(conns, deskey);
|
|
return retConn;
|
|
}
|
|
else
|
|
{
|
|
var url = APT.Infrastructure.Api.ConfigurationManager.AppSettings["ConnApiUrl"];
|
|
if (!string.IsNullOrEmpty(url))
|
|
{
|
|
url += $"?code={tenantName}&deskey={deskey}&tenantKey={tenantConnKey}";
|
|
var deData = WebUtils.Get(url, "");
|
|
//LoggerManager.GetLogger().Info($"TenantData:{deData}");
|
|
return EncryptHelper.AesDecrypt(deData, deskey);
|
|
}
|
|
}
|
|
return ConfigurationManager.ConnectionStrings["default"];
|
|
}
|
|
|
|
/// <summary>
|
|
/// 解密连接
|
|
/// </summary>
|
|
/// <param name="tenantName"></param>
|
|
/// <returns></returns>
|
|
public static string DesDbConn(string conn)
|
|
{
|
|
//redis取數據
|
|
var deskey = APT.Infrastructure.Api.ConfigurationManager.AppSettings["ConnDataKey"];
|
|
if (string.IsNullOrEmpty(deskey))
|
|
deskey = "optenergy2021001";
|
|
//LoggerManager.GetLogger().Info($"TenantData:{deData}");
|
|
return EncryptHelper.AesDecrypt(conn, deskey);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 加密连接
|
|
/// </summary>
|
|
/// <param name="conn"></param>
|
|
/// <returns></returns>
|
|
public static string EnDbConn(string conn)
|
|
{
|
|
//redis取數據
|
|
var deskey = APT.Infrastructure.Api.ConfigurationManager.AppSettings["ConnDataKey"];
|
|
if (string.IsNullOrEmpty(deskey))
|
|
deskey = "optenergy2021001";
|
|
//LoggerManager.GetLogger().Info($"TenantData:{deData}");
|
|
return EncryptHelper.AesEncrypt(conn, deskey);
|
|
}
|
|
}
|
|
}
|