using System; using System.Text.Json; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.Extensions.Caching.Distributed; namespace YYApi.Helpers { /// /// 认证信息获取中间件 /// public class AuthMiddleware { private RequestDelegate Next { get; } public AuthMiddleware(RequestDelegate next) { Next = next; } public Task Invoke(HttpContext httpContext, IDistributedCache cache) { string token = null; if (httpContext.Request.Headers.ContainsKey("Token")) { token = httpContext.Request.Headers["Token"]; } if (!string.IsNullOrEmpty(token)) { var login = cache.GetString(token); if (login != null) { httpContext.Items.Add("LoginInfo", JsonSerializer.Deserialize(login)); } } return Next(httpContext); } } /// /// 登录信息中间件 /// public class LoginInfoItem { //登录用户的ID public int Id { get; set; } //登录用户来源 public string Type { get; set; } } /// /// 检测登录中间件 /// public class CheckAuthAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext context) { if (context.HttpContext.Items["LoginId"] == null) { throw new ApiException(401); } } } /// /// 认证助手 /// public class Auth { private IDistributedCache Cache { get; } public Auth(IDistributedCache cache) { Cache = cache; } /// /// 产生一个Token记录并返回Token /// /// /// public string SetId(int id, string type = "user") { var uuid = GetUUID(); var loginItem = new LoginInfoItem { Id = id, Type = type }; Cache.SetString(uuid, JsonSerializer.Serialize(loginItem)); return uuid; } private string GetUUID() { return Guid.NewGuid().ToString(); } } }