From 8344567ac68bb2b0f6f51b1d10bedb07aa55ca63 Mon Sep 17 00:00:00 2001 From: xRain Date: Sun, 24 Mar 2024 17:31:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=BA=86=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E7=99=BB=E9=99=86=E4=BF=A1=E6=81=AF=E7=9A=84?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Helpers/SimApiAuth.cs | 15 +++++++++++++++ Middlewares/SimApiAuthMiddleware.cs | 7 ++++--- 2 files changed, 19 insertions(+), 3 deletions(-) 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); } }