mirror of https://github.com/abpframework/abp.git
Browse Source
Warn if MultiTenancyMiddleware is used before authenticationEngincanV/update-tui-editor
committed by
GitHub
1 changed files with 25 additions and 3 deletions
@ -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…
Reference in new issue