diff --git a/docs/en/ui-themes/lepton-x/blazor.md b/docs/en/ui-themes/lepton-x/blazor.md index 07d07eea00..9f5ee973ca 100644 --- a/docs/en/ui-themes/lepton-x/blazor.md +++ b/docs/en/ui-themes/lepton-x/blazor.md @@ -383,20 +383,18 @@ You can add a new `LeptonXFooter.razor` file and override the `Footer` component
``` +Only include footer links that point at real pages in your solution (for example `/PrivacyPolicy` and `/CookiePolicy` when those pages exist). Do not use placeholder `href="#"` links. + If you are using **Top Menu Layout**, then you can change the namespace as follows: ```diff diff --git a/framework/src/Volo.Abp.Security/Volo/Abp/Security/Claims/RemoteDynamicClaimsPrincipalContributorCacheBase.cs b/framework/src/Volo.Abp.Security/Volo/Abp/Security/Claims/RemoteDynamicClaimsPrincipalContributorCacheBase.cs index 23b2fbd56b..5d2f13352d 100644 --- a/framework/src/Volo.Abp.Security/Volo/Abp/Security/Claims/RemoteDynamicClaimsPrincipalContributorCacheBase.cs +++ b/framework/src/Volo.Abp.Security/Volo/Abp/Security/Claims/RemoteDynamicClaimsPrincipalContributorCacheBase.cs @@ -45,6 +45,24 @@ public abstract class RemoteDynamicClaimsPrincipalContributorCacheBase(() => cache.GetAsync(userId)); + + exception.Message.ShouldContain(userId.ToString()); + exception.Message.ShouldContain("from cache after refreshing"); + cache.RefreshCallCount.ShouldBe(2); + } + + private class TestRemoteDynamicClaimsPrincipalContributorCache + : RemoteDynamicClaimsPrincipalContributorCacheBase + { + private readonly int _missCountAfterRefresh; + private readonly AbpDynamicClaimCacheItem _cacheItem; + private int _getAfterRefreshCount; + + public int RefreshCallCount { get; private set; } + + public TestRemoteDynamicClaimsPrincipalContributorCache( + IOptions options, + int missCountAfterRefresh, + AbpDynamicClaimCacheItem cacheItem) + : base(options) + { + _missCountAfterRefresh = missCountAfterRefresh; + _cacheItem = cacheItem; + } + + protected override Task GetCacheAsync(Guid userId, Guid? tenantId = null) + { + if (RefreshCallCount == 0) + { + return Task.FromResult(null); + } + + _getAfterRefreshCount++; + if (_getAfterRefreshCount <= _missCountAfterRefresh) + { + return Task.FromResult(null); + } + + return Task.FromResult(_cacheItem); + } + + protected override Task RefreshAsync(Guid userId, Guid? tenantId = null) + { + RefreshCallCount++; + return Task.CompletedTask; + } + } +} diff --git a/modules/account/src/Volo.Abp.Account.Application/Volo/Abp/Account/DynamicClaimsAppService.cs b/modules/account/src/Volo.Abp.Account.Application/Volo/Abp/Account/DynamicClaimsAppService.cs index e26dbfc979..d928b7aaa1 100644 --- a/modules/account/src/Volo.Abp.Account.Application/Volo/Abp/Account/DynamicClaimsAppService.cs +++ b/modules/account/src/Volo.Abp.Account.Application/Volo/Abp/Account/DynamicClaimsAppService.cs @@ -25,7 +25,9 @@ public class DynamicClaimsAppService : IdentityAppServiceBase, IDynamicClaimsApp public virtual async Task RefreshAsync() { - await IdentityDynamicClaimsPrincipalContributorCache.ClearAsync(CurrentUser.GetId(), CurrentUser.TenantId); + // Overwrite the cache in place. Do not Clear then refill: parallel refresh requests + // would leave a gap where remote clients read null and reject the auth cookie. + await IdentityDynamicClaimsPrincipalContributorCache.RefreshAsync(CurrentUser.GetId(), CurrentUser.TenantId); await AbpClaimsPrincipalFactory.CreateDynamicAsync(PrincipalAccessor.Principal); } } diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityDynamicClaimsPrincipalContributorCache.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityDynamicClaimsPrincipalContributorCache.cs index c0b5023386..44ba5b5fc5 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityDynamicClaimsPrincipalContributorCache.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityDynamicClaimsPrincipalContributorCache.cs @@ -63,25 +63,7 @@ public class IdentityDynamicClaimsPrincipalContributorCache : ITransientDependen return await DynamicClaimCache.GetOrAddAsync(AbpDynamicClaimCacheItem.CalculateCacheKey(userId, tenantId), async () => { Logger.LogDebug($"Filling dynamic claims cache for user: {userId}"); - - var user = await UserManager.GetByIdAsync(userId); - var principal = await UserClaimsPrincipalFactory.CreateAsync(user); - - var dynamicClaims = new AbpDynamicClaimCacheItem(); - foreach (var claimType in AbpClaimsPrincipalFactoryOptions.Value.DynamicClaims) - { - var claims = principal.Claims.Where(x => x.Type == claimType).ToList(); - if (claims.Any()) - { - dynamicClaims.Claims.AddRange(claims.Select(claim => new AbpDynamicClaim(claimType, claim.Value))); - } - else - { - dynamicClaims.Claims.Add(new AbpDynamicClaim(claimType, null)); - } - } - - return dynamicClaims; + return await CreateCacheItemAsync(userId); }, () => new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = CacheOptions.Value.CacheAbsoluteExpiration @@ -89,9 +71,62 @@ public class IdentityDynamicClaimsPrincipalContributorCache : ITransientDependen } } + /// + /// Recomputes and overwrites the cached dynamic claims for the user without removing the + /// existing item first. Concurrent refresh requests must not observe an empty cache window + /// (Clear + refill races reject cookies on tiered / microservice clients). + /// + public virtual async Task RefreshAsync(Guid userId, Guid? tenantId = null) + { + Logger.LogDebug($"Refresh dynamic claims cache for user: {userId}"); + + using (CurrentTenant.Change(tenantId)) + { + AbpDynamicClaimCacheItem cacheItem; + if (AbpClaimsPrincipalFactoryOptions.Value.DynamicClaims.IsNullOrEmpty()) + { + cacheItem = new AbpDynamicClaimCacheItem(); + } + else + { + cacheItem = await CreateCacheItemAsync(userId); + } + + await DynamicClaimCache.SetAsync( + AbpDynamicClaimCacheItem.CalculateCacheKey(userId, tenantId), + cacheItem, + new DistributedCacheEntryOptions + { + AbsoluteExpirationRelativeToNow = CacheOptions.Value.CacheAbsoluteExpiration + }); + } + } + public virtual async Task ClearAsync(Guid userId, Guid? tenantId = null) { Logger.LogDebug($"Remove dynamic claims cache for user: {userId}"); await DynamicClaimCache.RemoveAsync(AbpDynamicClaimCacheItem.CalculateCacheKey(userId, tenantId)); } + + protected virtual async Task CreateCacheItemAsync(Guid userId) + { + var user = await UserManager.GetByIdAsync(userId); + var principal = await UserClaimsPrincipalFactory.CreateAsync(user); + + var dynamicClaims = new AbpDynamicClaimCacheItem(); + foreach (var claimType in AbpClaimsPrincipalFactoryOptions.Value.DynamicClaims) + { + var claims = principal.Claims.Where(x => x.Type == claimType).ToList(); + if (claims.Any()) + { + dynamicClaims.Claims.AddRange(claims.Select(claim => new AbpDynamicClaim(claimType, claim.Value))); + } + else + { + dynamicClaims.Claims.Add(new AbpDynamicClaim(claimType, null)); + } + } + + return dynamicClaims; + } } diff --git a/modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdentityDynamicClaimsPrincipalContributorCache_Tests.cs b/modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdentityDynamicClaimsPrincipalContributorCache_Tests.cs new file mode 100644 index 0000000000..1d5e0c72ee --- /dev/null +++ b/modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdentityDynamicClaimsPrincipalContributorCache_Tests.cs @@ -0,0 +1,50 @@ +using System.Threading.Tasks; +using Shouldly; +using Volo.Abp.Caching; +using Volo.Abp.Security.Claims; +using Xunit; + +namespace Volo.Abp.Identity; + +public class IdentityDynamicClaimsPrincipalContributorCache_Tests : AbpIdentityDomainTestBase +{ + private readonly IdentityDynamicClaimsPrincipalContributorCache _cache; + private readonly IDistributedCache _distributedCache; + private readonly IdentityTestData _testData; + + public IdentityDynamicClaimsPrincipalContributorCache_Tests() + { + _cache = GetRequiredService(); + _distributedCache = GetRequiredService>(); + _testData = GetRequiredService(); + } + + [Fact] + public async Task RefreshAsync_Should_Overwrite_Cache_Without_Clearing_First() + { + var cacheKey = AbpDynamicClaimCacheItem.CalculateCacheKey(_testData.UserJohnId, null); + + await _distributedCache.SetAsync(cacheKey, new AbpDynamicClaimCacheItem()); + + await _cache.RefreshAsync(_testData.UserJohnId); + + var item = await _distributedCache.GetAsync(cacheKey); + item.ShouldNotBeNull(); + item.Claims.ShouldNotBeEmpty(); + } + + [Fact] + public async Task RefreshAsync_Should_Keep_Cache_Populated_Across_Parallel_Calls() + { + var cacheKey = AbpDynamicClaimCacheItem.CalculateCacheKey(_testData.UserJohnId, null); + + await Task.WhenAll( + _cache.RefreshAsync(_testData.UserJohnId), + _cache.RefreshAsync(_testData.UserJohnId), + _cache.RefreshAsync(_testData.UserJohnId)); + + var item = await _distributedCache.GetAsync(cacheKey); + item.ShouldNotBeNull(); + item.Claims.ShouldNotBeEmpty(); + } +}