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 c84751245..c4ff72b74 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 @@ -76,7 +76,19 @@ await IdentitySessionStore.CreateAsync( await IdentityDynamicClaimsPrincipalContributorCache.ClearAsync(userId.Value, tenantId); - await IdentitySessionCache.RefreshAsync(sessionId, + // 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( device, deviceDesc, @@ -86,7 +98,9 @@ await IdentitySessionCache.RefreshAsync(sessionId, clientIpAddress, Clock.Now, Clock.Now, - deviceInfo.IpRegion)); + deviceInfo.IpRegion, + expiraIn), + cancellationToken); } } } diff --git a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Domain/System/Security/Principal/AbpClaimsIdentityExpiraInExtensions.cs b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Domain/System/Security/Principal/AbpClaimsIdentityExpiraInExtensions.cs new file mode 100644 index 000000000..9dee7eaa2 --- /dev/null +++ b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Domain/System/Security/Principal/AbpClaimsIdentityExpiraInExtensions.cs @@ -0,0 +1,36 @@ +using JetBrains.Annotations; +using System.Linq; +using System.Security.Claims; +using Volo.Abp; + +namespace System.Security.Principal; +public static class AbpClaimsIdentityExpiraInExtensions +{ + public static long? FindExpirainTime([NotNull] this ClaimsPrincipal principal) + { + return principal.FindLongClaimValue("exp"); + } + + public static long? FindIssuedTime([NotNull] this ClaimsPrincipal principal) + { + return principal.FindLongClaimValue("iat"); + } + + public static long? FindLongClaimValue([NotNull] this ClaimsPrincipal principal, string claimType) + { + Check.NotNull(principal, nameof(principal)); + + var longValueOrNull = principal.Claims?.FirstOrDefault(c => c.Type == claimType); + if (longValueOrNull == null || longValueOrNull.Value.IsNullOrWhiteSpace()) + { + return null; + } + + if (long.TryParse(longValueOrNull.Value, out var longValue)) + { + return longValue; + } + + return null; + } +} diff --git a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Session.AspNetCore/LINGYUN/Abp/Identity/Session/AspNetCore/AbpIdentitySessionAspNetCoreModule.cs b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Session.AspNetCore/LINGYUN/Abp/Identity/Session/AspNetCore/AbpIdentitySessionAspNetCoreModule.cs index a751b428b..31d0a0f79 100644 --- a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Session.AspNetCore/LINGYUN/Abp/Identity/Session/AspNetCore/AbpIdentitySessionAspNetCoreModule.cs +++ b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Session.AspNetCore/LINGYUN/Abp/Identity/Session/AspNetCore/AbpIdentitySessionAspNetCoreModule.cs @@ -1,4 +1,6 @@ using LINGYUN.Abp.IP2Region; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; using Volo.Abp.AspNetCore; using Volo.Abp.Modularity; @@ -9,4 +11,8 @@ namespace LINGYUN.Abp.Identity.Session.AspNetCore; [DependsOn(typeof(AbpIdentitySessionModule))] public class AbpIdentitySessionAspNetCoreModule : AbpModule { + public override void ConfigureServices(ServiceConfigurationContext context) + { + context.Services.Replace(ServiceDescriptor.Singleton()); + } } diff --git a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Session.AspNetCore/LINGYUN/Abp/Identity/Session/AspNetCore/IP2RegionLocationInfoProvider.cs b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Session.AspNetCore/LINGYUN/Abp/Identity/Session/AspNetCore/IP2RegionLocationInfoProvider.cs index df515050d..a9177aec1 100644 --- a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Session.AspNetCore/LINGYUN/Abp/Identity/Session/AspNetCore/IP2RegionLocationInfoProvider.cs +++ b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Session.AspNetCore/LINGYUN/Abp/Identity/Session/AspNetCore/IP2RegionLocationInfoProvider.cs @@ -1,10 +1,10 @@ using IP2Region.Net.Abstractions; using System; using System.Threading.Tasks; -using Volo.Abp.DependencyInjection; namespace LINGYUN.Abp.Identity.Session.AspNetCore; -public class IP2RegionLocationInfoProvider : IIpLocationInfoProvider, ISingletonDependency + +public class IP2RegionLocationInfoProvider : IIpLocationInfoProvider { protected static readonly LocationInfo _nullCache = null; diff --git a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Session/LINGYUN/Abp/Identity/Session/DefaultIdentitySessionCache.cs b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Session/LINGYUN/Abp/Identity/Session/DefaultIdentitySessionCache.cs index 3104b2d00..0067026b7 100644 --- a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Session/LINGYUN/Abp/Identity/Session/DefaultIdentitySessionCache.cs +++ b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Session/LINGYUN/Abp/Identity/Session/DefaultIdentitySessionCache.cs @@ -1,5 +1,7 @@ -using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Caching.Distributed; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; +using System; using System.Threading; using System.Threading.Tasks; using Volo.Abp.Caching; @@ -9,6 +11,7 @@ namespace LINGYUN.Abp.Identity.Session; public class DefaultIdentitySessionCache : IIdentitySessionCache, ITransientDependency { public ILogger Logger { protected get; set; } + protected IDistributedCache Cache { get; } public DefaultIdentitySessionCache(IDistributedCache cache) @@ -31,8 +34,16 @@ public async virtual Task RefreshAsync(string sessionId, IdentitySessionCacheIte Logger.LogDebug($"Refresh user session cache for: {sessionId}"); var cacheKey = IdentitySessionCacheItem.CalculateCacheKey(sessionId); - - await Cache.SetAsync(cacheKey, cacheItem, token: cancellationToken); + DistributedCacheEntryOptions cacheOptions = null; + if (cacheItem.ExpiraIn.HasValue) + { + cacheOptions = new DistributedCacheEntryOptions + { + AbsoluteExpirationRelativeToNow = TimeSpan.FromMilliseconds(cacheItem.ExpiraIn.Value), + }; + } + + await Cache.SetAsync(cacheKey, cacheItem, options: cacheOptions, token: cancellationToken); } public async virtual Task RemoveAsync(string sessionId, CancellationToken cancellationToken = default) diff --git a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Session/LINGYUN/Abp/Identity/Session/IdentitySessionCacheItem.cs b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Session/LINGYUN/Abp/Identity/Session/IdentitySessionCacheItem.cs index d6d5e90af..05328cede 100644 --- a/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Session/LINGYUN/Abp/Identity/Session/IdentitySessionCacheItem.cs +++ b/aspnet-core/modules/identity/LINGYUN.Abp.Identity.Session/LINGYUN/Abp/Identity/Session/IdentitySessionCacheItem.cs @@ -6,23 +6,46 @@ namespace LINGYUN.Abp.Identity.Session; public class IdentitySessionCacheItem { private const string CacheKeyFormat = "s:{0}"; - + /// + /// 登录设备 + /// public string Device { get; set; } - + /// + /// 设备描述 + /// public string DeviceInfo { get; set; } - + /// + /// 用户Id + /// public Guid UserId { get; set; } - + /// + /// 会话Id + /// public string SessionId { get; set; } - + /// + /// 客户端Id + /// public string ClientId { get; set; } - + /// + /// IP地址 + /// public string IpAddresses { get; set; } + /// + /// IP属地 + /// public string IpRegion { get; set; } - + /// + /// 登录时间 + /// public DateTime SignedIn { get; set; } - + /// + /// 上次访问时间 + /// public DateTime? LastAccessed { get; set; } + /// + /// 过期时间(ms) + /// + public double? ExpiraIn { get; set; } public IdentitySessionCacheItem() { @@ -37,7 +60,8 @@ public IdentitySessionCacheItem( string ipAddresses, DateTime signedIn, DateTime? lastAccessed = null, - string ipRegion = null) + string ipRegion = null, + double? expiraIn = null) { Device = device; DeviceInfo = deviceInfo; @@ -48,6 +72,7 @@ public IdentitySessionCacheItem( IpRegion = ipRegion; SignedIn = signedIn; LastAccessed = lastAccessed; + ExpiraIn = expiraIn; } public static string CalculateCacheKey(string sessionId)