Files
simapi-net/Helpers/SimApiAuth.cs
T

144 lines
4.4 KiB
C#
Raw Normal View History

2020-02-04 16:51:15 +08:00
using System;
using System.Collections.Generic;
2020-02-04 16:51:15 +08:00
using Microsoft.Extensions.Caching.Distributed;
2020-08-05 15:29:56 +08:00
using SimApi.Communications;
using StackExchange.Redis;
2020-02-04 16:51:15 +08:00
2024-03-23 07:29:43 +08:00
namespace SimApi.Helpers;
/// <summary>
/// 认证助手
/// </summary>
public class SimApiAuth(IDistributedCache cache, IConnectionMultiplexer redis)
2020-02-04 16:51:15 +08:00
{
private const string TokenCacheKey = "SimApi:Auth:Token:{token}";
private const string TokenSetCacheKey = "SimApi:Auth:User:{userId}";
private readonly IDatabase _redisDb = redis.GetDatabase();
2020-02-04 16:51:15 +08:00
/// <summary>
2024-08-19 03:39:35 +08:00
/// 登录信息
2020-02-04 16:51:15 +08:00
/// </summary>
2024-08-19 03:39:35 +08:00
/// <param name="loginItem"></param>
/// <param name="expireTime"></param>
2024-03-24 17:31:22 +08:00
/// <param name="token"></param>
2024-03-23 07:29:43 +08:00
/// <returns></returns>
public string Login(SimApiLoginItem loginItem, TimeSpan? expireTime = null, string? token = null)
2020-02-04 16:51:15 +08:00
{
expireTime ??= TimeSpan.FromDays(7);
2024-08-19 03:39:35 +08:00
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);
2024-08-19 03:39:35 +08:00
return token;
2024-06-27 04:38:04 +08:00
}
2025-10-25 17:12:39 +08:00
/// <summary>
/// 更新登陆信息
/// </summary>
/// <param name="loginItem"></param>
/// <param name="token"></param>
/// <returns></returns>
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);
2025-10-25 17:12:39 +08:00
return token;
}
2024-03-24 17:31:22 +08:00
/// <summary>
/// 获取登陆信息
/// </summary>
/// <param name="token"></param>
/// <returns></returns>
public SimApiLoginItem? GetLogin(string token)
{
var cacheKey = TokenCacheKey.Replace("{token}", token);
var login = cache.GetString(cacheKey);
var resp = login != null ? SimApiUtil.FromJson<SimApiLoginItem>(login) : null;
if (resp != null)
{
var ttl = _redisDb.KeyTimeToLive(cacheKey);
var setCacheKey = TokenSetCacheKey.Replace("{userId}", resp.Id);
_redisDb.KeyExpire(setCacheKey, ttl);
}
return resp;
}
/// <summary>
/// 获取所有的登录token
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
public SimApiLoginItem[] GetAllLogins(string userId)
{
var setCacheKey = TokenSetCacheKey.Replace("{userId}", userId);
var allLogins = _redisDb.SetMembers(setCacheKey).ToStringArray();
var resp = new List<SimApiLoginItem>();
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();
}
/// <summary>
/// 退出所有登录
/// </summary>
/// <param name="userId"></param>
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);
2024-03-24 17:31:22 +08:00
}
2024-03-23 07:29:43 +08:00
/// <summary>
/// 退出登陆
/// </summary>
/// <param name="token">登陆标识</param>
public void Logout(string token)
2024-03-23 07:29:43 +08:00
{
var item = GetLogin(token);
if (item != null)
2020-02-18 08:07:51 +08:00
{
var setCacheKey = TokenSetCacheKey.Replace("{userId}", item.Id);
_redisDb.SetRemove(setCacheKey, token);
}
var cacheKey = TokenCacheKey.Replace("{token}", token);
cache.Remove(cacheKey);
2020-02-04 16:51:15 +08:00
}
2023-03-28 06:33:48 +08:00
}