From bef6e5381999d13978155b5506ed4cd216a2090d Mon Sep 17 00:00:00 2001 From: zpc Date: Tue, 3 Sep 2024 22:10:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HuanMeng.MiaoYu.Code/Base/MiaoYuBase.cs | 36 ++++++++++++------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/src/0-core/HuanMeng.MiaoYu.Code/Base/MiaoYuBase.cs b/src/0-core/HuanMeng.MiaoYu.Code/Base/MiaoYuBase.cs index 8fbfd96..121b0b4 100644 --- a/src/0-core/HuanMeng.MiaoYu.Code/Base/MiaoYuBase.cs +++ b/src/0-core/HuanMeng.MiaoYu.Code/Base/MiaoYuBase.cs @@ -238,23 +238,33 @@ namespace HuanMeng.MiaoYu.Code.Base } if (!string.IsNullOrEmpty(accessToken)) { - var (principal, jwtToken) = JwtAuthManager.DecodeJwtToken(accessToken); - if (jwtToken == null)//|| !jwtToken.Header.Alg.Equals(SecurityAlgorithms.HmacSha256Signature) + try { - throw new SecurityTokenException("无效的token"); + var (principal, jwtToken) = JwtAuthManager.DecodeJwtToken(accessToken); + if (jwtToken == null)//|| !jwtToken.Header.Alg.Equals(SecurityAlgorithms.HmacSha256Signature) + { + throw new SecurityTokenException("无效的token"); + } + var userIdStr = principal.FindFirst("UserId")?.Value; + if (string.IsNullOrEmpty(userIdStr)) + { + throw new SecurityTokenException("无效的token"); + } + var nickName = principal.FindFirst("NickName")?.Value; + var userId = int.Parse(userIdStr); + this._userInfo = new RequestUserInfo() + { + UserId = userId, + NickName = nickName + }; } - var userIdStr = principal.FindFirst("UserId")?.Value; - if (string.IsNullOrEmpty(userIdStr)) + catch (Exception) { - throw new SecurityTokenException("无效的token"); + _userInfo = new RequestUserInfo() + { + UserId = 0 + }; } - var nickName = principal.FindFirst("NickName")?.Value; - var userId = int.Parse(userIdStr); - this._userInfo = new RequestUserInfo() - { - UserId = userId, - NickName = nickName - }; } else {