diff --git a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Domain/LINGYUN/Abp/Identity/Session/IdentitySessionManager.cs b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Domain/LINGYUN/Abp/Identity/Session/IdentitySessionManager.cs index c4ff72b74..8ab6cdc32 100644 --- a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Domain/LINGYUN/Abp/Identity/Session/IdentitySessionManager.cs +++ b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Domain/LINGYUN/Abp/Identity/Session/IdentitySessionManager.cs @@ -7,7 +7,6 @@ using System.Threading.Tasks; using Volo.Abp.Auditing; using Volo.Abp.Domain.Services; using Volo.Abp.Identity; -using Volo.Abp.Timing; namespace LINGYUN.Abp.Identity.Session; public class IdentitySessionManager : DomainService, IIdentitySessionManager @@ -76,17 +75,6 @@ public class IdentitySessionManager : DomainService, IIdentitySessionManager await IdentityDynamicClaimsPrincipalContributorCache.ClearAsync(userId.Value, tenantId); - // 2024-10-10 从令牌中取颁布时间与过期时间计算时间戳,作为默认缓存过期时间 - double? expiraIn = null; - var expirainTime = claimsPrincipal.FindExpirainTime(); - var issuedTime = claimsPrincipal.FindIssuedTime(); - if (expirainTime.HasValue && issuedTime.HasValue) - { - expiraIn = DateTimeOffset.FromUnixTimeMilliseconds(expirainTime.Value) - .Subtract(DateTimeOffset.FromUnixTimeMilliseconds(issuedTime.Value)) - .TotalMicroseconds; - } - await IdentitySessionCache.RefreshAsync( sessionId, new IdentitySessionCacheItem( @@ -98,8 +86,7 @@ public class IdentitySessionManager : DomainService, IIdentitySessionManager clientIpAddress, Clock.Now, Clock.Now, - deviceInfo.IpRegion, - expiraIn), + deviceInfo.IpRegion), cancellationToken); } } diff --git a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Session.AspNetCore/LINGYUN/Abp/Identity/Session/AspNetCore/AbpIdentitySessionDynamicClaimsPrincipalContributor.cs b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Session.AspNetCore/LINGYUN/Abp/Identity/Session/AspNetCore/AbpIdentitySessionDynamicClaimsPrincipalContributor.cs index 2f8146ed7..79b10be7e 100644 --- a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Session.AspNetCore/LINGYUN/Abp/Identity/Session/AspNetCore/AbpIdentitySessionDynamicClaimsPrincipalContributor.cs +++ b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Session.AspNetCore/LINGYUN/Abp/Identity/Session/AspNetCore/AbpIdentitySessionDynamicClaimsPrincipalContributor.cs @@ -24,7 +24,7 @@ public class AbpIdentitySessionDynamicClaimsPrincipalContributor : IAbpDynamicCl using (currentTenant.Change(tenantId)) { var identitySessionChecker = context.GetRequiredService(); - if (!await identitySessionChecker.ValidateSessionAsync(sessionId)) + if (!await identitySessionChecker.ValidateSessionAsync(context.ClaimsPrincipal)) { // 用户会话已过期 context.ClaimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity()); diff --git a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Session/LINGYUN/Abp/Identity/Session/DefaultIdentitySessionChecker.cs b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Session/LINGYUN/Abp/Identity/Session/DefaultIdentitySessionChecker.cs index 9fc016392..0b0f69dfd 100644 --- a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Session/LINGYUN/Abp/Identity/Session/DefaultIdentitySessionChecker.cs +++ b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Session/LINGYUN/Abp/Identity/Session/DefaultIdentitySessionChecker.cs @@ -2,6 +2,8 @@ using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; using System; +using System.Security.Claims; +using System.Security.Principal; using System.Threading; using System.Threading.Tasks; using Volo.Abp.DependencyInjection; @@ -40,8 +42,9 @@ public class DefaultIdentitySessionChecker : IIdentitySessionChecker, ITransient Logger = NullLogger.Instance; } - public async virtual Task ValidateSessionAsync(string sessionId, CancellationToken cancellationToken = default) + public async virtual Task ValidateSessionAsync(ClaimsPrincipal claimsPrincipal, CancellationToken cancellationToken = default) { + var sessionId = claimsPrincipal.FindSessionId(); if (sessionId.IsNullOrWhiteSpace()) { Logger.LogDebug("No user session id found."); @@ -67,6 +70,15 @@ public class DefaultIdentitySessionChecker : IIdentitySessionChecker, ITransient identitySessionCacheItem.LastAccessed = accressedTime; identitySessionCacheItem.IpAddresses = DeviceInfoProvider.ClientIpAddress; + // 2024-10-10 从令牌中取颁布时间与过期时间计算时间戳,作为默认缓存过期时间 + var expirainTime = claimsPrincipal.FindExpirainTime(); + var timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); + if (expirainTime.HasValue) + { + // 2024-10-25 应计算剩余过期时间 + identitySessionCacheItem.ExpiraIn = (expirainTime.Value - timestamp) * 1000; + } + Logger.LogDebug($"Refresh the user access info in the cache from {sessionId}."); await IdentitySessionCache.RefreshAsync(sessionId, identitySessionCacheItem, cancellationToken); } diff --git a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Session/LINGYUN/Abp/Identity/Session/IIdentitySessionChecker.cs b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Session/LINGYUN/Abp/Identity/Session/IIdentitySessionChecker.cs index d21dae5f5..13f2176b6 100644 --- a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Session/LINGYUN/Abp/Identity/Session/IIdentitySessionChecker.cs +++ b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Session/LINGYUN/Abp/Identity/Session/IIdentitySessionChecker.cs @@ -1,8 +1,9 @@ -using System.Threading; +using System.Security.Claims; +using System.Threading; using System.Threading.Tasks; namespace LINGYUN.Abp.Identity.Session; public interface IIdentitySessionChecker { - Task ValidateSessionAsync(string sessionId, CancellationToken cancellationToken = default); + Task ValidateSessionAsync(ClaimsPrincipal claimsPrincipal, CancellationToken cancellationToken = default); } diff --git a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Domain/System/Security/Principal/AbpClaimsIdentityExpiraInExtensions.cs b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Session/System/Security/Principal/AbpClaimsIdentityExpiraInExtensions.cs similarity index 100% rename from aspnet-core/modules/identity/LINGYUN.Abp.Identity.Domain/System/Security/Principal/AbpClaimsIdentityExpiraInExtensions.cs rename to aspnet-core/modules/identity/LINGYUN.Abp.Identity.Session/System/Security/Principal/AbpClaimsIdentityExpiraInExtensions.cs diff --git a/aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.AspNetCore.Session/LINGYUN/Abp/OpenIddict/AspNetCore/Session/UserinfoIdentitySession.cs b/aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.AspNetCore.Session/LINGYUN/Abp/OpenIddict/AspNetCore/Session/UserinfoIdentitySession.cs index 45eb48f1b..f0c54e17e 100644 --- a/aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.AspNetCore.Session/LINGYUN/Abp/OpenIddict/AspNetCore/Session/UserinfoIdentitySession.cs +++ b/aspnet-core/modules/openIddict/LINGYUN.Abp.OpenIddict.AspNetCore.Session/LINGYUN/Abp/OpenIddict/AspNetCore/Session/UserinfoIdentitySession.cs @@ -35,11 +35,9 @@ public class UserinfoIdentitySession : IOpenIddictServerHandler 401 // Errors.ExpiredToken ---> 400