From c059f136cfebfe5fbbc932fe3b9491b6bbbee82b Mon Sep 17 00:00:00 2001 From: maliming Date: Tue, 8 Jul 2025 18:09:45 +0800 Subject: [PATCH] Warn only if CurrentUserTenantResolveContributor is used --- ...ultiTenancyApplicationBuilderExtensions.cs | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore.MultiTenancy/Microsoft/AspNetCore/Builder/AbpAspNetCoreMultiTenancyApplicationBuilderExtensions.cs b/framework/src/Volo.Abp.AspNetCore.MultiTenancy/Microsoft/AspNetCore/Builder/AbpAspNetCoreMultiTenancyApplicationBuilderExtensions.cs index 5c7aebeb7a..0eaaadd784 100644 --- a/framework/src/Volo.Abp.AspNetCore.MultiTenancy/Microsoft/AspNetCore/Builder/AbpAspNetCoreMultiTenancyApplicationBuilderExtensions.cs +++ b/framework/src/Volo.Abp.AspNetCore.MultiTenancy/Microsoft/AspNetCore/Builder/AbpAspNetCoreMultiTenancyApplicationBuilderExtensions.cs @@ -1,6 +1,8 @@ -using Microsoft.Extensions.DependencyInjection; +using System.Linq; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Volo.Abp.AspNetCore.MultiTenancy; +using Volo.Abp.MultiTenancy; namespace Microsoft.AspNetCore.Builder; @@ -11,14 +13,22 @@ public static class AbpAspNetCoreMultiTenancyApplicationBuilderExtensions public static IApplicationBuilder UseMultiTenancy(this IApplicationBuilder app) { - var authenticationMiddlewareSet = app.Properties.TryGetValue(AuthenticationMiddlewareSetKey, out var value) && value is true; - if (!authenticationMiddlewareSet) + var multiTenancyOptions = app.ApplicationServices.GetRequiredService(); + var hasCurrentUserTenantResolveContributor = multiTenancyOptions.TenantResolvers.Any(r => r is CurrentUserTenantResolveContributor); + if (hasCurrentUserTenantResolveContributor) { - var logger = app.ApplicationServices.GetService>(); - logger?.LogWarning("MultiTenancyMiddleware is being registered before the authentication middleware. " + - "This may lead to incorrect tenant resolution. " + - "Ensure that app.UseAuthentication() is called before app.UseMultiTenancy()."); + var authenticationMiddlewareSet = app.Properties.TryGetValue(AuthenticationMiddlewareSetKey, out var value) && value is true; + if (!authenticationMiddlewareSet) + { + var logger = app.ApplicationServices.GetService>(); + logger?.LogWarning( + "MultiTenancyMiddleware is being registered before the authentication middleware. " + + "This may lead to incorrect tenant resolution if the resolution depends on the authenticated user. " + + "Ensure app.UseAuthentication() is called before app.UseMultiTenancy()." + ); + } } + return app.UseMiddleware(); } }