using System; using System.Text.Json; using Microsoft.Extensions.Caching.Distributed; using SimApi.Communications; namespace SimApi.Helpers; /// /// 认证助手 /// public class SimApiAuth(IDistributedCache cache) { /// /// 产生一个Token记录并返回Token /// /// /// public string Login(string id, string type = "user", string token = null) { return Login(id, new[] { type }, token); } /// /// 产生一个Token并记录用户ID角色[多角色] /// /// /// /// public string Login(string id, string[] type, string uuid = null) { uuid ??= Guid.NewGuid().ToString(); var loginItem = new SimApiLoginItem(id, type); cache.SetString(uuid, JsonSerializer.Serialize(loginItem)); return uuid; } /// /// 退出登陆 /// /// 登陆标识 public void Logout(string uuid) { if (!string.IsNullOrEmpty(uuid)) { cache.Remove(uuid); } } }