You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

已配置ABP子域名多租户,与常规请求的差异是什么?

子域名多租户配置后的请求差异解析

背景配置信息

客户端Program.cs核心配置

string val = await SubDomainTenant.GetApiServerAuthorityWithTenantSubDomain(builder, serviceProvider); // = https://en.api.localhost.com:44367
builder.Services.Configure<AbpRemoteServiceOptions>(options =>
{
    options.RemoteServices.Default = new RemoteServiceConfiguration(val);
});

服务端核心配置

builder.Services.Configure<AbpTenantResolveOptions>(options =>
{
    options.AddDomainTenantResolver(builder.Configuration["App:SelfUrl"]);
});

客户端配置文件

"App": {
  "SelfUrl": "https://{0}.localhost.com:44367"
},
"RemoteServices": {
  "Default": {
    "BaseUrl": "https://{0}.api.localhost.com:44367"
  }
}

服务端配置文件

"RootUrl": "https://{0}.localhost.com:44367",
"App": {
  "SelfUrl": "https://{0}.api.localhost.com:44367",
  "SelfUrlWithoutTenant": "https://api.localhost.com:44367",
  "CorsOrigins": "https://*.localhost.com,http://*.localhost.com:45144,https://*.localhost.com:44367,http://localhost.com:45144,https://localhost.com:44367"
}

客户端补充代码

string val = await SubDomainTenant.GetApiServerAuthorityWithTenantSubDomain(builder, serviceProvider);
builder.Services.Configure<AbpRemoteServiceOptions>(options =>
{
    options.RemoteServices.Default = new RemoteServiceConfiguration(val);
});

var host = builder.Build();

CultureInfo culture;

Settings settings = await host.Services.GetRequiredService<Task<Settings>>();
var tenant = SubDomainTenant.GetTenantName(builder.HostEnvironment.BaseAddress);
if (tenant != null)
    tenant = tenant.Substring(0, tenant.IndexOf('.'));
if (tenant != null)
{    
    culture = new CultureInfo(tenant);
}
else
{    
    culture = new CultureInfo("en");
    var configUrl = settings.App.SelfUrl;
    var address = configUrl.Replace("{0}", tenant);
    Uri uri1 = new Uri(address);
    navigator.NavigateTo(address, true);
}

配置后请求与常规请求的核心差异

1. 租户标识传递方式不同

  • 常规请求:一般通过请求头(如__tenant)、URL参数或请求体携带租户信息,服务端需手动从这些位置解析租户。
  • 当前配置请求:通过**子域名前缀en**传递租户标识,服务端依托DomainTenantResolver自动从请求域名的子域名部分提取租户名称(比如从en.localhost.com提取en),无需额外在请求中携带租户信息。

2. 请求地址绑定逻辑不同

  • 常规请求:HttpClient的BaseUrl固定(如https://api.localhost.com:44367),所有请求拼接固定地址,不会自动关联租户。
  • 当前配置请求:
    • 客户端通过SubDomainTenant.GetApiServerAuthorityWithTenantSubDomain动态生成带租户子域名的BaseUrl(https://en.api.localhost.com:44367),并配置到AbpRemoteServiceOptions。
    • 即便你调用httpClient.GetStringAsync("api/account/getkeyiv")的写法和常规请求一致,ABP框架底层已自动将请求地址绑定到带租户子域名的服务端地址,服务端最终收到的https://en.localhost.com:44367/api/account/getkeyiv已包含租户标识,无需手动处理租户路由。

3. 多租户隔离实现逻辑不同

  • 常规请求:需在代码中手动处理租户的路由隔离、数据过滤,比如每个请求手动添加租户参数,服务端手动校验租户权限。
  • 当前配置请求:
    • 服务端通过DomainTenantResolver自动完成租户解析,ABP框架会基于解析出的租户自动进行数据隔离、权限校验,无需在业务代码中手动处理租户逻辑。
    • 客户端域名(en.localhost.com)和服务端域名(en.api.localhost.com)通过子域名前缀统一租户标识,实现前后端租户关联,确保同一租户的前后端请求自动绑定到对应租户上下文。

4. 域名路由跳转逻辑不同

  • 常规请求:若用户访问不带租户的域名,需手动编写跳转逻辑引导到默认租户域名。
  • 当前配置请求:客户端补充代码已实现自动跳转——如果无法从当前域名解析租户,会自动跳转到默认租户(en)的域名(https://en.localhost.com:44367),确保用户始终处于某个租户的上下文环境中。

内容的提问来源于stack exchange,提问作者mz1378

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 07:11:01