Browse Source

Merge pull request #23264 from abpframework/MultiTenancyMiddleware

Warn if MultiTenancyMiddleware is used before authentication
EngincanV/update-tui-editor
Engincan VESKE 7 months ago
committed by GitHub
parent
commit
403372de10
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 28
      framework/src/Volo.Abp.AspNetCore.MultiTenancy/Microsoft/AspNetCore/Builder/AbpAspNetCoreMultiTenancyApplicationBuilderExtensions.cs

28
framework/src/Volo.Abp.AspNetCore.MultiTenancy/Microsoft/AspNetCore/Builder/AbpAspNetCoreMultiTenancyApplicationBuilderExtensions.cs

@ -1,12 +1,34 @@
using Volo.Abp.AspNetCore.MultiTenancy;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Volo.Abp.AspNetCore.MultiTenancy;
using Volo.Abp.MultiTenancy;
namespace Microsoft.AspNetCore.Builder;
public static class AbpAspNetCoreMultiTenancyApplicationBuilderExtensions
{
private const string AuthenticationMiddlewareSetKey = "__AuthenticationMiddlewareSet";
public static IApplicationBuilder UseMultiTenancy(this IApplicationBuilder app)
{
return app
.UseMiddleware<MultiTenancyMiddleware>();
var multiTenancyOptions = app.ApplicationServices.GetRequiredService<IOptions<AbpTenantResolveOptions>>();
var hasCurrentUserTenantResolveContributor = multiTenancyOptions.Value.TenantResolvers.Any(r => r is CurrentUserTenantResolveContributor);
if (hasCurrentUserTenantResolveContributor)
{
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