Browse Source

Merge remote-tracking branch 'origin/rel-10.7' into auto-merge-forward/rel-10.7-to-rel-11.0-175

pull/26468/head
github-actions[bot] 7 days ago
parent
commit
4af20db4ce
  1. 12
      docs/en/ui-themes/lepton-x/blazor.md
  2. 18
      framework/src/Volo.Abp.Security/Volo/Abp/Security/Claims/RemoteDynamicClaimsPrincipalContributorCacheBase.cs
  3. 84
      framework/test/Volo.Abp.Security.Tests/Volo/Abp/Security/Claims/RemoteDynamicClaimsPrincipalContributorCacheBase_Tests.cs
  4. 4
      modules/account/src/Volo.Abp.Account.Application/Volo/Abp/Account/DynamicClaimsAppService.cs
  5. 73
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityDynamicClaimsPrincipalContributorCache.cs
  6. 50
      modules/identity/test/Volo.Abp.Identity.Domain.Tests/Volo/Abp/Identity/IdentityDynamicClaimsPrincipalContributorCache_Tests.cs

12
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
<div class="lpx-footbar-container">
<div class="lpx-footbar">
<div class="lpx-footbar-copyright">
<span>@DateTime.UtcNow.Year©</span>
<a href="https://leptontheme.com/" target="_blank">Lepton ThemeXXX</a>
<span>by</span>
<a href="https://volosoft.com/" target="_blank">Volosoft</a>
<span>@DateTime.UtcNow.Year © MyProjectName</span>
</div>
<div class="lpx-footbar-solo-links">
<a href="#">About</a>
<a href="#">Privacy</a>
<a href="#">Contact</a>
<a href="/PrivacyPolicy">Privacy</a>
<a href="/CookiePolicy">Cookie Policy</a>
</div>
</div>
</div>
```
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

18
framework/src/Volo.Abp.Security/Volo/Abp/Security/Claims/RemoteDynamicClaimsPrincipalContributorCacheBase.cs

@ -45,6 +45,24 @@ public abstract class RemoteDynamicClaimsPrincipalContributorCacheBase<TContribu
}
dynamicClaims = await GetCacheAsync(userId, tenantId);
if (dynamicClaims == null)
{
// A concurrent clear (session revoke / claim invalidation) or another refresh can
// remove the item between the remote refill and this read. Retry once before failing.
Logger.LogDebug($"Dynamic claims cache for user: {userId} is still empty after refresh, retrying once.");
try
{
await refresh();
}
catch (Exception e)
{
Logger.LogWarning(e, $"Failed to refresh remote claims for user: {userId}");
throw;
}
dynamicClaims = await GetCacheAsync(userId, tenantId);
}
if (dynamicClaims == null)
{
throw new AbpException($"Failed to get dynamic claims for user: {userId} from cache after refreshing, " +

84
framework/test/Volo.Abp.Security.Tests/Volo/Abp/Security/Claims/RemoteDynamicClaimsPrincipalContributorCacheBase_Tests.cs

@ -0,0 +1,84 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using Shouldly;
using Xunit;
namespace Volo.Abp.Security.Claims;
public class RemoteDynamicClaimsPrincipalContributorCacheBase_Tests
{
[Fact]
public async Task GetAsync_Should_Retry_Refresh_When_Cache_Is_Empty_After_First_Refresh()
{
var userId = Guid.NewGuid();
var cacheItem = new AbpDynamicClaimCacheItem();
var cache = new TestRemoteDynamicClaimsPrincipalContributorCache(
Microsoft.Extensions.Options.Options.Create(new AbpClaimsPrincipalFactoryOptions()),
missCountAfterRefresh: 1,
cacheItem);
var result = await cache.GetAsync(userId);
result.ShouldBe(cacheItem);
cache.RefreshCallCount.ShouldBe(2);
}
[Fact]
public async Task GetAsync_Should_Throw_When_Cache_Is_Still_Empty_After_Retry()
{
var userId = Guid.NewGuid();
var cache = new TestRemoteDynamicClaimsPrincipalContributorCache(
Microsoft.Extensions.Options.Options.Create(new AbpClaimsPrincipalFactoryOptions()),
missCountAfterRefresh: 2,
cacheItem: new AbpDynamicClaimCacheItem());
var exception = await Assert.ThrowsAsync<AbpException>(() => cache.GetAsync(userId));
exception.Message.ShouldContain(userId.ToString());
exception.Message.ShouldContain("from cache after refreshing");
cache.RefreshCallCount.ShouldBe(2);
}
private class TestRemoteDynamicClaimsPrincipalContributorCache
: RemoteDynamicClaimsPrincipalContributorCacheBase<TestRemoteDynamicClaimsPrincipalContributorCache>
{
private readonly int _missCountAfterRefresh;
private readonly AbpDynamicClaimCacheItem _cacheItem;
private int _getAfterRefreshCount;
public int RefreshCallCount { get; private set; }
public TestRemoteDynamicClaimsPrincipalContributorCache(
IOptions<AbpClaimsPrincipalFactoryOptions> options,
int missCountAfterRefresh,
AbpDynamicClaimCacheItem cacheItem)
: base(options)
{
_missCountAfterRefresh = missCountAfterRefresh;
_cacheItem = cacheItem;
}
protected override Task<AbpDynamicClaimCacheItem?> GetCacheAsync(Guid userId, Guid? tenantId = null)
{
if (RefreshCallCount == 0)
{
return Task.FromResult<AbpDynamicClaimCacheItem?>(null);
}
_getAfterRefreshCount++;
if (_getAfterRefreshCount <= _missCountAfterRefresh)
{
return Task.FromResult<AbpDynamicClaimCacheItem?>(null);
}
return Task.FromResult<AbpDynamicClaimCacheItem?>(_cacheItem);
}
protected override Task RefreshAsync(Guid userId, Guid? tenantId = null)
{
RefreshCallCount++;
return Task.CompletedTask;
}
}
}

4
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);
}
}

73
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
}
}
/// <summary>
/// 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).
/// </summary>
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<AbpDynamicClaimCacheItem> 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;
}
}

50
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<AbpDynamicClaimCacheItem> _distributedCache;
private readonly IdentityTestData _testData;
public IdentityDynamicClaimsPrincipalContributorCache_Tests()
{
_cache = GetRequiredService<IdentityDynamicClaimsPrincipalContributorCache>();
_distributedCache = GetRequiredService<IDistributedCache<AbpDynamicClaimCacheItem>>();
_testData = GetRequiredService<IdentityTestData>();
}
[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();
}
}
Loading…
Cancel
Save