using System; using System.Collections.Generic; using Microsoft.Extensions.Caching.Distributed; using SimApi.Communications; using StackExchange.Redis; namespace SimApi.Helpers; /// /// 认证助手 /// public class SimApiAuth(IDistributedCache cache, IConnectionMultiplexer redis) { private const string TokenCacheKey = "SimApi:Auth:Token:{token}"; private const string TokenSetCacheKey = "SimApi:Auth:User:{userId}"; private readonly IDatabase _redisDb = redis.GetDatabase(); /// /// 登录信息 /// /// /// /// /// public string Login(SimApiLoginItem loginItem, TimeSpan? expireTime = null, string? token = null) { expireTime ??= TimeSpan.FromDays(7); token ??= Guid.NewGuid().ToString(); var cacheKey = TokenCacheKey.Replace("{token}", token); var setCacheKey = TokenSetCacheKey.Replace("{userId}", loginItem.Id); _redisDb.SetAdd(setCacheKey, token); cache.SetString(cacheKey, SimApiUtil.Json(loginItem), new DistributedCacheEntryOptions { SlidingExpiration = expireTime }); _redisDb.KeyExpire(setCacheKey, expireTime.Value); return token; } /// /// 更新登陆信息 /// /// /// /// public string Update(SimApiLoginItem loginItem, string token) { var cacheKey = TokenCacheKey.Replace("{token}", token); cache.SetString(cacheKey, SimApiUtil.Json(loginItem)); var ttl = _redisDb.KeyTimeToLive(cacheKey); var setCacheKey = TokenSetCacheKey.Replace("{userId}", loginItem.Id); _redisDb.KeyExpire(setCacheKey, ttl); return token; } /// /// 获取登陆信息 /// /// /// public SimApiLoginItem? GetLogin(string token) { var cacheKey = TokenCacheKey.Replace("{token}", token); var login = cache.GetString(cacheKey); var resp = login != null ? SimApiUtil.FromJson(login) : null; if (resp != null) { var ttl = _redisDb.KeyTimeToLive(cacheKey); var setCacheKey = TokenSetCacheKey.Replace("{userId}", resp.Id); _redisDb.KeyExpire(setCacheKey, ttl); } return resp; } /// /// 获取所有的登录token /// /// /// public SimApiLoginItem[] GetAllLogins(string userId) { var setCacheKey = TokenSetCacheKey.Replace("{userId}", userId); var allLogins = _redisDb.SetMembers(setCacheKey).ToStringArray(); var resp = new List(); foreach (var login in allLogins) { if (login != null) { var item = GetLogin(login); if (item != null) { resp.Add(item); } else { _redisDb.SetRemove(setCacheKey, login); } } } return resp.ToArray(); } /// /// 退出所有登录 /// /// public void LogoutAll(string userId) { var setCacheKey = TokenSetCacheKey.Replace("{userId}", userId); var allLogins = _redisDb.SetMembers(setCacheKey).ToStringArray(); foreach (var login in allLogins) { if (login != null) { var cacheKey = TokenCacheKey.Replace("{token}", login); cache.Remove(cacheKey); } } _redisDb.KeyDelete(setCacheKey); } /// /// 退出登陆 /// /// 登陆标识 public void Logout(string token) { var item = GetLogin(token); if (item != null) { var setCacheKey = TokenSetCacheKey.Replace("{userId}", item.Id); _redisDb.SetRemove(setCacheKey, token); } var cacheKey = TokenCacheKey.Replace("{token}", token); cache.Remove(cacheKey); } }