Browse Source

Warn only if CurrentUserTenantResolveContributor is used

pull/23264/head
maliming 7 months ago
parent
commit
c059f136cf
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 24
      framework/src/Volo.Abp.AspNetCore.MultiTenancy/Microsoft/AspNetCore/Builder/AbpAspNetCoreMultiTenancyApplicationBuilderExtensions.cs

24
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<AbpTenantResolveOptions>();
var hasCurrentUserTenantResolveContributor = multiTenancyOptions.TenantResolvers.Any(r => r is CurrentUserTenantResolveContributor);
if (hasCurrentUserTenantResolveContributor)
{
var logger = app.ApplicationServices.GetService<ILogger<MultiTenancyMiddleware>>();
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<ILogger<MultiTenancyMiddleware>>();
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<MultiTenancyMiddleware>();
}
}

Loading…
Cancel
Save