diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/ClientProxyExceptionEventHandler.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/ClientProxyExceptionEventHandler.cs index b3614064f9..81f3357bfb 100644 --- a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/ClientProxyExceptionEventHandler.cs +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/ClientProxyExceptionEventHandler.cs @@ -24,34 +24,46 @@ public class ClientProxyExceptionEventHandler : ILocalEventHandler>(); - - if (!options.Value.IsBlazorWebApp) + case 401: { - var navigationManager = scope.ServiceProvider.GetRequiredService(); - var accessTokenProvider = scope.ServiceProvider.GetRequiredService(); - var authenticationOptions = scope.ServiceProvider.GetRequiredService>(); - var result = await accessTokenProvider.RequestAccessToken(); - if (result.Status != AccessTokenResultStatus.Success) + var options = scope.ServiceProvider.GetRequiredService>(); + + if (!options.Value.IsBlazorWebApp) { - navigationManager.NavigateToLogout(authenticationOptions.Value.LogoutUrl); - return; - } + var navigationManager = scope.ServiceProvider.GetRequiredService(); + var accessTokenProvider = scope.ServiceProvider.GetRequiredService(); + var authenticationOptions = scope.ServiceProvider.GetRequiredService>(); + var result = await accessTokenProvider.RequestAccessToken(); + if (result.Status != AccessTokenResultStatus.Success) + { + navigationManager.NavigateToLogout(authenticationOptions.Value.LogoutUrl); + return; + } - result.TryGetToken(out var token); - if (token != null && DateTimeOffset.Now >= token.Expires.AddMinutes(-5)) + result.TryGetToken(out var token); + if (token != null && DateTimeOffset.Now >= token.Expires.AddMinutes(-5)) + { + navigationManager.NavigateToLogout(authenticationOptions.Value.LogoutUrl); + } + } + else { - navigationManager.NavigateToLogout(authenticationOptions.Value.LogoutUrl); + var jsRuntime = scope.ServiceProvider.GetRequiredService(); + await jsRuntime.InvokeVoidAsync("eval", "setTimeout(function(){location.assign('/')}, 2000)"); } + + break; } - else + case 403: { var jsRuntime = scope.ServiceProvider.GetRequiredService(); await jsRuntime.InvokeVoidAsync("eval", "setTimeout(function(){location.assign('/')}, 2000)"); + + break; } } } diff --git a/framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/Authentication/AbpAuthenticationHubFilter.cs b/framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/Authentication/AbpAuthenticationHubFilter.cs index baac1b538f..c7b8c029cf 100644 --- a/framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/Authentication/AbpAuthenticationHubFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.SignalR/Volo/Abp/AspNetCore/SignalR/Authentication/AbpAuthenticationHubFilter.cs @@ -13,7 +13,9 @@ public class AbpAuthenticationHubFilter : IHubFilter public virtual async ValueTask InvokeMethodAsync(HubInvocationContext invocationContext, Func> next) { var currentPrincipalAccessor = invocationContext.ServiceProvider.GetRequiredService(); - using (currentPrincipalAccessor.Change((await GetDynamicClaimsPrincipalAsync(invocationContext.Context.User, invocationContext.ServiceProvider))!)) + var claimsPrincipal = invocationContext.Context.User; + await HandleDynamicClaimsPrincipalAsync(claimsPrincipal, invocationContext.ServiceProvider, invocationContext.Context); + using (currentPrincipalAccessor.Change(claimsPrincipal!)) { return await next(invocationContext); } @@ -22,7 +24,9 @@ public class AbpAuthenticationHubFilter : IHubFilter public virtual async Task OnConnectedAsync(HubLifetimeContext context, Func next) { var currentPrincipalAccessor = context.ServiceProvider.GetRequiredService(); - using (currentPrincipalAccessor.Change((await GetDynamicClaimsPrincipalAsync(context.Context.User, context.ServiceProvider))!)) + var claimsPrincipal = context.Context.User; + await HandleDynamicClaimsPrincipalAsync(claimsPrincipal, context.ServiceProvider, context.Context); + using (currentPrincipalAccessor.Change(claimsPrincipal!)) { await next(context); } @@ -31,27 +35,29 @@ public class AbpAuthenticationHubFilter : IHubFilter public virtual async Task OnDisconnectedAsync(HubLifetimeContext context, Exception? exception, Func next) { var currentPrincipalAccessor = context.ServiceProvider.GetRequiredService(); - using (currentPrincipalAccessor.Change((await GetDynamicClaimsPrincipalAsync(context.Context.User, context.ServiceProvider))!)) + var claimsPrincipal = context.Context.User; + await HandleDynamicClaimsPrincipalAsync(claimsPrincipal, context.ServiceProvider, context.Context); + using (currentPrincipalAccessor.Change(claimsPrincipal!)) { await next(context, exception); } } - protected virtual async Task GetDynamicClaimsPrincipalAsync(ClaimsPrincipal? claimsPrincipal, IServiceProvider serviceProvider) + protected virtual async Task HandleDynamicClaimsPrincipalAsync(ClaimsPrincipal? claimsPrincipal, IServiceProvider serviceProvider, HubCallerContext hubCallerContext) { - if (claimsPrincipal == null) - { - return claimsPrincipal; - } - - if (claimsPrincipal.Identity != null && + if (claimsPrincipal?.Identity != null && claimsPrincipal.Identity.IsAuthenticated && serviceProvider.GetRequiredService>().Value.IsDynamicClaimsEnabled) { - var abpClaimsPrincipalFactory = serviceProvider.GetRequiredService(); - claimsPrincipal = await abpClaimsPrincipalFactory.CreateDynamicAsync(claimsPrincipal); - } + claimsPrincipal = claimsPrincipal.Identity is ClaimsIdentity identity + ? new ClaimsPrincipal(new ClaimsIdentity(claimsPrincipal.Claims, claimsPrincipal.Identity.AuthenticationType, identity.NameClaimType, identity.RoleClaimType)) + : new ClaimsPrincipal(new ClaimsIdentity(claimsPrincipal.Claims, claimsPrincipal.Identity.AuthenticationType)); - return claimsPrincipal; + claimsPrincipal = await serviceProvider.GetRequiredService().CreateDynamicAsync(claimsPrincipal); + if (claimsPrincipal.Identity?.IsAuthenticated == false) + { + hubCallerContext.Abort(); + } + } } }