using System;
using System.Text.Json;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Caching.Distributed;
using SimApi.Communications;
namespace SimApi.Helpers
{
///
/// 认证助手
///
public class SimApiAuth
{
private IDistributedCache Cache { get; }
public SimApiAuth(IDistributedCache cache)
{
Cache = 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)
{
if (uuid == null)
{
uuid = Guid.NewGuid().ToString();
}
var loginItem = new SimApiLoginItem
{
Id = id,
Type = type
};
Cache.SetString(uuid, JsonSerializer.Serialize(loginItem));
return uuid;
}
///
/// 退出登陆
///
/// 登陆标识
public void Logout(string uuid)
{
if (!string.IsNullOrEmpty(uuid))
{
Cache.Remove(uuid);
}
}
}
}