From 929fc246dbe532b27dcd1ad2a672cf1bbefdaba1 Mon Sep 17 00:00:00 2001 From: maliming Date: Mon, 11 Mar 2024 16:21:34 +0800 Subject: [PATCH] Clear the `ApplicationConfigurationDto` cache when getting dynamic claims fails. --- .../MvcCachedApplicationConfigurationClientHelper.cs | 2 +- .../RemoteDynamicClaimsPrincipalContributorCache.cs | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) rename framework/src/{Volo.Abp.AspNetCore.Mvc.Client => Volo.Abp.AspNetCore.Mvc.Client.Common}/Volo/Abp/AspNetCore/Mvc/Client/MvcCachedApplicationConfigurationClientHelper.cs (83%) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.Client/Volo/Abp/AspNetCore/Mvc/Client/MvcCachedApplicationConfigurationClientHelper.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.Client.Common/Volo/Abp/AspNetCore/Mvc/Client/MvcCachedApplicationConfigurationClientHelper.cs similarity index 83% rename from framework/src/Volo.Abp.AspNetCore.Mvc.Client/Volo/Abp/AspNetCore/Mvc/Client/MvcCachedApplicationConfigurationClientHelper.cs rename to framework/src/Volo.Abp.AspNetCore.Mvc.Client.Common/Volo/Abp/AspNetCore/Mvc/Client/MvcCachedApplicationConfigurationClientHelper.cs index 740a79c884..cc1180fd20 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.Client/Volo/Abp/AspNetCore/Mvc/Client/MvcCachedApplicationConfigurationClientHelper.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.Client.Common/Volo/Abp/AspNetCore/Mvc/Client/MvcCachedApplicationConfigurationClientHelper.cs @@ -3,7 +3,7 @@ using Volo.Abp.Users; namespace Volo.Abp.AspNetCore.Mvc.Client; -internal static class MvcCachedApplicationConfigurationClientHelper +public static class MvcCachedApplicationConfigurationClientHelper { public static string CreateCacheKey(ICurrentUser currentUser) { diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.Client.Common/Volo/Abp/AspNetCore/Mvc/Client/RemoteDynamicClaimsPrincipalContributorCache.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.Client.Common/Volo/Abp/AspNetCore/Mvc/Client/RemoteDynamicClaimsPrincipalContributorCache.cs index 738884fe06..f63c04e49c 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.Client.Common/Volo/Abp/AspNetCore/Mvc/Client/RemoteDynamicClaimsPrincipalContributorCache.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.Client.Common/Volo/Abp/AspNetCore/Mvc/Client/RemoteDynamicClaimsPrincipalContributorCache.cs @@ -4,10 +4,12 @@ using System.Net.Http; using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; +using Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations; using Volo.Abp.Caching; using Volo.Abp.Http.Client; using Volo.Abp.Http.Client.Authentication; using Volo.Abp.Security.Claims; +using Volo.Abp.Users; namespace Volo.Abp.AspNetCore.Mvc.Client; @@ -18,17 +20,23 @@ public class RemoteDynamicClaimsPrincipalContributorCache : RemoteDynamicClaimsP protected IDistributedCache Cache { get; } protected IHttpClientFactory HttpClientFactory { get; } protected IRemoteServiceHttpClientAuthenticator HttpClientAuthenticator { get; } + protected IDistributedCache ApplicationConfigurationDtoCache { get; } + protected ICurrentUser CurrentUser { get; } public RemoteDynamicClaimsPrincipalContributorCache( IDistributedCache cache, IHttpClientFactory httpClientFactory, IOptions abpClaimsPrincipalFactoryOptions, - IRemoteServiceHttpClientAuthenticator httpClientAuthenticator) + IRemoteServiceHttpClientAuthenticator httpClientAuthenticator, + IDistributedCache applicationConfigurationDtoCache, + ICurrentUser currentUser) : base(abpClaimsPrincipalFactoryOptions) { Cache = cache; HttpClientFactory = httpClientFactory; HttpClientAuthenticator = httpClientAuthenticator; + ApplicationConfigurationDtoCache = applicationConfigurationDtoCache; + CurrentUser = currentUser; } protected async override Task GetCacheAsync(Guid userId, Guid? tenantId = null) @@ -49,6 +57,7 @@ public class RemoteDynamicClaimsPrincipalContributorCache : RemoteDynamicClaimsP catch (Exception e) { Logger.LogWarning(e, $"Failed to refresh remote claims for user: {userId}"); + await ApplicationConfigurationDtoCache.RemoveAsync(MvcCachedApplicationConfigurationClientHelper.CreateCacheKey(CurrentUser)); throw; } }