diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.Client/Volo/Abp/AspNetCore/Mvc/Client/MvcCachedApplicationConfigurationClient.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.Client/Volo/Abp/AspNetCore/Mvc/Client/MvcCachedApplicationConfigurationClient.cs index 8478cc49cb..97f4bddf8e 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.Client/Volo/Abp/AspNetCore/Mvc/Client/MvcCachedApplicationConfigurationClient.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.Client/Volo/Abp/AspNetCore/Mvc/Client/MvcCachedApplicationConfigurationClient.cs @@ -3,6 +3,8 @@ using System.Globalization; using Microsoft.AspNetCore.Http; using System.Threading.Tasks; using Microsoft.Extensions.Caching.Distributed; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; using Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations; using Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations.ClientProxies; @@ -18,6 +20,8 @@ public class MvcCachedApplicationConfigurationClient : ICachedApplicationConfigu { private const string HttpContextItemsCacheKeyFormat = "ApplicationConfigurationDto_{0}_{1}_CacheKey"; + public ILogger Logger { get; set; } + protected IHttpContextAccessor HttpContextAccessor { get; } protected AbpApplicationConfigurationClientProxy ApplicationConfigurationAppService { get; } protected AbpApplicationLocalizationClientProxy ApplicationLocalizationClientProxy { get; } @@ -35,6 +39,8 @@ public class MvcCachedApplicationConfigurationClient : ICachedApplicationConfigu AbpApplicationLocalizationClientProxy applicationLocalizationClientProxy, IOptions options) { + Logger = NullLogger.Instance; + ApplicationConfigurationAppService = applicationConfigurationAppService; CurrentUser = currentUser; HttpContextAccessor = httpContextAccessor; @@ -68,14 +74,37 @@ public class MvcCachedApplicationConfigurationClient : ICachedApplicationConfigu return configuration; } - configuration = (await Cache.GetOrAddAsync( + var cached = await Cache.GetAsync(cacheKey); + if (cached != null) + { + if (httpContext != null) + { + httpContext.Items[cacheKey] = cached; + } + + return cached; + } + + configuration = await GetRemoteConfigurationAsync(); + + // The cache key comes from ICurrentUser, but the access token comes from the HttpContext. A Blazor Server circuit can + // still run as the user after its request has ended, so the call goes without a token and gets an anonymous configuration. + if (!IsForCurrentUser(configuration)) + { + Logger.LogWarning( + "Not caching the remote application configuration: it is for user {RemoteUserId} (authenticated: {RemoteIsAuthenticated}), not the current user {CurrentUserId}.", + configuration.CurrentUser?.Id, configuration.CurrentUser?.IsAuthenticated, CurrentUser.Id); + return configuration; + } + + await Cache.SetAsync( cacheKey, - async () => await GetRemoteConfigurationAsync(), - () => new DistributedCacheEntryOptions + configuration, + new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = Options.ApplicationConfigurationDtoCacheAbsoluteExpiration } - ))!; + ); if (httpContext != null) { @@ -85,6 +114,17 @@ public class MvcCachedApplicationConfigurationClient : ICachedApplicationConfigu return configuration; } + protected virtual bool IsForCurrentUser(ApplicationConfigurationDto configuration) + { + var user = configuration.CurrentUser; + if (user == null) + { + return !CurrentUser.IsAuthenticated; + } + + return user.IsAuthenticated == CurrentUser.IsAuthenticated && user.Id == CurrentUser.Id; + } + protected virtual async Task GetRemoteConfigurationAsync() { var cultureName = CultureInfo.CurrentUICulture.Name; diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Client.Tests/Volo/Abp/AspNetCore/Mvc/Client/MvcCachedApplicationConfigurationClient_Tests.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Client.Tests/Volo/Abp/AspNetCore/Mvc/Client/MvcCachedApplicationConfigurationClient_Tests.cs index 6784c69954..30da41b48f 100644 --- a/framework/test/Volo.Abp.AspNetCore.Mvc.Client.Tests/Volo/Abp/AspNetCore/Mvc/Client/MvcCachedApplicationConfigurationClient_Tests.cs +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Client.Tests/Volo/Abp/AspNetCore/Mvc/Client/MvcCachedApplicationConfigurationClient_Tests.cs @@ -1,4 +1,6 @@ +using System; using System.Collections.Generic; +using System.Security.Claims; using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; @@ -7,6 +9,7 @@ using Shouldly; using Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations; using Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations.ClientProxies; using Volo.Abp.Localization; +using Volo.Abp.Security.Claims; using Xunit; namespace Volo.Abp.AspNetCore.Mvc.Client; @@ -92,14 +95,45 @@ public class MvcCachedApplicationConfigurationClient_Tests : AbpAspNetCoreMvcCli } } - private static ApplicationConfigurationDto CreateConfigDto(string cultureName) + [Fact] + public async Task Should_Not_Cache_An_Anonymous_Configuration_For_A_Signed_In_User() + { + var userId = Guid.NewGuid(); + + using (CultureHelper.Use("en")) + using (SignIn(userId)) + { + // A call without the user's access token (a Blazor Server circuit after its request ended) is answered anonymously. + _configProxy.GetAsync(Arg.Any()).Returns(CreateConfigDto("en")); + _localizationProxy.GetAsync(Arg.Any()).Returns(new ApplicationLocalizationDto { Resources = new() }); + + (await _applicationConfigurationClient.GetAsync()).CurrentUser.IsAuthenticated.ShouldBeFalse(); + + // The next call asks again, and keeps the user's own configuration. + _configProxy.GetAsync(Arg.Any()).Returns(CreateConfigDto("en", userId)); + + (await _applicationConfigurationClient.GetAsync()).CurrentUser.Id.ShouldBe(userId); + (await _applicationConfigurationClient.GetAsync()).CurrentUser.Id.ShouldBe(userId); + + await _configProxy.Received(2).GetAsync(Arg.Any()); + } + } + + private IDisposable SignIn(Guid userId) + { + return GetRequiredService().Change(new ClaimsPrincipal(new ClaimsIdentity( + new[] { new Claim(AbpClaimTypes.UserId, userId.ToString()) }, "Test"))); + } + + private static ApplicationConfigurationDto CreateConfigDto(string cultureName, Guid? userId = null) { return new ApplicationConfigurationDto { Localization = { CurrentCulture = new CurrentCultureDto { Name = cultureName } - } + }, + CurrentUser = new CurrentUserDto { IsAuthenticated = userId.HasValue, Id = userId } }; } }