更新 JWT
This commit is contained in:
1 parent
f84c4c1949
commit
8526e5e0d2
10 files changed
+40
-63
No files matched your search
@@ -1,4 +1,4 @@
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace StoreHub.Framework.Security;
|
||||
@@ -11,7 +11,7 @@ public class OnlineUserStore : IOnlineUserStore
|
||||
private const string CacheKeyPrefix = "online:user:";
|
||||
private readonly IMemoryCache _cache;
|
||||
// 在线人员索引:IMemoryCache 不提供公共枚举 API,这里用字典维护便于查询在线列表
|
||||
private readonly ConcurrentDictionary<int, OnlineUserInfo> _onlineIndex = new();
|
||||
private readonly ConcurrentDictionary<string, OnlineUserInfo> _onlineIndex = new(StringComparer.Ordinal);
|
||||
|
||||
public OnlineUserStore(IMemoryCache cache)
|
||||
{
|
||||
@@ -23,33 +23,33 @@ public class OnlineUserStore : IOnlineUserStore
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(info);
|
||||
|
||||
// 以 userId 为键覆盖写入:旧会话(jti)被顶替,实现单点登录
|
||||
_cache.Set(GetKey(info.UserId), info, new MemoryCacheEntryOptions
|
||||
// 以登录账户为键覆盖写入:旧会话(jti)被顶替,实现单点登录
|
||||
_cache.Set(GetKey(info.UserName), info, new MemoryCacheEntryOptions
|
||||
{
|
||||
// 缓存条目与令牌同时过期,过期后自动失效
|
||||
AbsoluteExpiration = info.ExpiresAt,
|
||||
});
|
||||
_onlineIndex[info.UserId] = info;
|
||||
_onlineIndex[info.UserName] = info;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool IsTokenActive(int userId, string jti)
|
||||
public bool IsTokenActive(string userName, string jti)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(jti))
|
||||
if (string.IsNullOrWhiteSpace(userName) || string.IsNullOrWhiteSpace(jti))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return _cache.TryGetValue(GetKey(userId), out OnlineUserInfo? info)
|
||||
return _cache.TryGetValue(GetKey(userName), out OnlineUserInfo? info)
|
||||
&& info is not null
|
||||
&& string.Equals(info.Jti, jti, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void SignOut(int userId)
|
||||
public void SignOut(string userName)
|
||||
{
|
||||
_cache.Remove(GetKey(userId));
|
||||
_onlineIndex.TryRemove(userId, out _);
|
||||
_cache.Remove(GetKey(userName));
|
||||
_onlineIndex.TryRemove(userName, out _);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -57,7 +57,7 @@ public class OnlineUserStore : IOnlineUserStore
|
||||
{
|
||||
DateTime now = DateTime.Now;
|
||||
List<OnlineUserInfo> result = [];
|
||||
foreach (KeyValuePair<int, OnlineUserInfo> pair in _onlineIndex)
|
||||
foreach (KeyValuePair<string, OnlineUserInfo> pair in _onlineIndex)
|
||||
{
|
||||
if (pair.Value.ExpiresAt > now)
|
||||
{
|
||||
@@ -75,6 +75,6 @@ public class OnlineUserStore : IOnlineUserStore
|
||||
/// <inheritdoc />
|
||||
public int Count => GetOnlineUsers().Count;
|
||||
|
||||
private static string GetKey(int userId) => $"{CacheKeyPrefix}{userId}";
|
||||
private static string GetKey(string userName) => $"{CacheKeyPrefix}{userName}";
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user