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