From 6a92781d107ff9fe91146540f7d15f1164086293 Mon Sep 17 00:00:00 2001 From: maliming Date: Tue, 8 Jul 2025 17:44:47 +0800 Subject: [PATCH 1/4] Warn if MultiTenancyMiddleware is used before authentication --- ...MultiTenancyApplicationBuilderExtensions.cs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 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 ceb872e5c3..5c7aebeb7a 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,12 +1,24 @@ -using Volo.Abp.AspNetCore.MultiTenancy; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Volo.Abp.AspNetCore.MultiTenancy; namespace Microsoft.AspNetCore.Builder; public static class AbpAspNetCoreMultiTenancyApplicationBuilderExtensions { + private const string AuthenticationMiddlewareSetKey = "__AuthenticationMiddlewareSet"; + + public static IApplicationBuilder UseMultiTenancy(this IApplicationBuilder app) { - return app - .UseMiddleware(); + 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. " + + "Ensure that app.UseAuthentication() is called before app.UseMultiTenancy()."); + } + return app.UseMiddleware(); } } From c059f136cfebfe5fbbc932fe3b9491b6bbbee82b Mon Sep 17 00:00:00 2001 From: maliming Date: Tue, 8 Jul 2025 18:09:45 +0800 Subject: [PATCH 2/4] 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(); } } From e518e37a1c083ec70fbefb35c1b476dbbddad621 Mon Sep 17 00:00:00 2001 From: maliming Date: Tue, 8 Jul 2025 18:11:02 +0800 Subject: [PATCH 3/4] Remove unnecessary blank line in extension class --- .../AbpAspNetCoreMultiTenancyApplicationBuilderExtensions.cs | 1 - 1 file changed, 1 deletion(-) 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 0eaaadd784..9a948863ea 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 @@ -10,7 +10,6 @@ public static class AbpAspNetCoreMultiTenancyApplicationBuilderExtensions { private const string AuthenticationMiddlewareSetKey = "__AuthenticationMiddlewareSet"; - public static IApplicationBuilder UseMultiTenancy(this IApplicationBuilder app) { var multiTenancyOptions = app.ApplicationServices.GetRequiredService(); From 016603d4e9f98d326e8d4a007942032a8ec31d70 Mon Sep 17 00:00:00 2001 From: maliming Date: Tue, 8 Jul 2025 19:39:07 +0800 Subject: [PATCH 4/4] Fix tenant resolve options DI to use IOptions --- .../AbpAspNetCoreMultiTenancyApplicationBuilderExtensions.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 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 9a948863ea..7ccd1a73c3 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,7 @@ using System.Linq; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; using Volo.Abp.AspNetCore.MultiTenancy; using Volo.Abp.MultiTenancy; @@ -12,8 +13,8 @@ public static class AbpAspNetCoreMultiTenancyApplicationBuilderExtensions public static IApplicationBuilder UseMultiTenancy(this IApplicationBuilder app) { - var multiTenancyOptions = app.ApplicationServices.GetRequiredService(); - var hasCurrentUserTenantResolveContributor = multiTenancyOptions.TenantResolvers.Any(r => r is CurrentUserTenantResolveContributor); + var multiTenancyOptions = app.ApplicationServices.GetRequiredService>(); + var hasCurrentUserTenantResolveContributor = multiTenancyOptions.Value.TenantResolvers.Any(r => r is CurrentUserTenantResolveContributor); if (hasCurrentUserTenantResolveContributor) { var authenticationMiddlewareSet = app.Properties.TryGetValue(AuthenticationMiddlewareSetKey, out var value) && value is true;