diff --git a/Helpers/SimApiAuth.cs b/Helpers/SimApiAuth.cs index f529518..25a34dd 100644 --- a/Helpers/SimApiAuth.cs +++ b/Helpers/SimApiAuth.cs @@ -1,3 +1,4 @@ +#nullable enable using System; using System.Text.Json; using Microsoft.Extensions.Caching.Distributed; @@ -14,6 +15,8 @@ public class SimApiAuth(IDistributedCache cache) /// 产生一个Token记录并返回Token /// /// + /// + /// /// public string Login(string id, string type = "user", string token = null) { @@ -25,6 +28,7 @@ public class SimApiAuth(IDistributedCache cache) /// /// /// + /// /// public string Login(string id, string[] type, string uuid = null) { @@ -34,6 +38,17 @@ public class SimApiAuth(IDistributedCache cache) return uuid; } + /// + /// 获取登陆信息 + /// + /// + /// + public SimApiLoginItem? GetLogin(string token) + { + var login = cache.GetString(token); + return login != null ? JsonSerializer.Deserialize(login) : default; + } + /// /// 退出登陆 /// diff --git a/Middlewares/SimApiAuthMiddleware.cs b/Middlewares/SimApiAuthMiddleware.cs index 8bf4cc0..809737c 100644 --- a/Middlewares/SimApiAuthMiddleware.cs +++ b/Middlewares/SimApiAuthMiddleware.cs @@ -3,6 +3,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Caching.Distributed; using SimApi.Communications; +using SimApi.Helpers; namespace SimApi.Middlewares; @@ -11,7 +12,7 @@ namespace SimApi.Middlewares; /// public class SimApiAuthMiddleware(RequestDelegate next) { - public Task Invoke(HttpContext httpContext, IDistributedCache cache) + public Task Invoke(HttpContext httpContext, IDistributedCache cache, SimApiAuth auth) { string token = null; if (httpContext.Request.Headers.TryGetValue("Token", out var header)) @@ -21,10 +22,10 @@ public class SimApiAuthMiddleware(RequestDelegate next) if (!string.IsNullOrEmpty(token)) { - var login = cache.GetString(token); + var login = auth.GetLogin(token); if (login != null) { - httpContext.Items.Add("LoginInfo", JsonSerializer.Deserialize(login)); + httpContext.Items.Add("LoginInfo", login); } }