diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/Claims/AbpDynamicClaimsMiddleware.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/Claims/AbpDynamicClaimsMiddleware.cs index d2c328f2ae..b2bbb78e7b 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/Claims/AbpDynamicClaimsMiddleware.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/Claims/AbpDynamicClaimsMiddleware.cs @@ -2,6 +2,7 @@ using System; using System.Threading.Tasks; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Features.Authentication; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Volo.Abp.AspNetCore.Middleware; @@ -18,9 +19,21 @@ public class AbpDynamicClaimsMiddleware : AbpMiddlewareBase, ITransientDependenc { if (context.RequestServices.GetRequiredService>().Value.IsDynamicClaimsEnabled) { - var authenticationType = context.User.Identity.AuthenticationType; - var abpClaimsPrincipalFactory = context.RequestServices.GetRequiredService(); - context.User = await abpClaimsPrincipalFactory.CreateDynamicAsync(context.User); + var authenticateResultFeature = context.Features.Get(); + var authenticationType = authenticateResultFeature?.AuthenticateResult?.Ticket?.AuthenticationScheme ?? context.User.Identity.AuthenticationType; + + if (authenticateResultFeature != null && !authenticationType.IsNullOrWhiteSpace()) + { + var abpClaimsPrincipalFactory = context.RequestServices.GetRequiredService(); + var user = await abpClaimsPrincipalFactory.CreateDynamicAsync(context.User); + + authenticateResultFeature.AuthenticateResult = AuthenticateResult.Success(new AuthenticationTicket(user, authenticationType)); + var httpAuthenticationFeature = context.Features.Get(); + if (httpAuthenticationFeature != null) + { + httpAuthenticationFeature.User = authenticateResultFeature.AuthenticateResult.Principal; + } + } if (context.User.Identity?.IsAuthenticated == false) {