From 2fcff63ee2ce5fb9c1a18f6dbc66f746e87efdff Mon Sep 17 00:00:00 2001 From: glz <694770232@qq.com> Date: Mon, 29 Jun 2026 17:49:28 +0800 Subject: [PATCH] fix RedisHelper wxTokenKeyPrefix --- .../Middleware/JwtAutoRefreshMiddleware.cs | 13 +++++++++---- .../WeChatAuthService.cs | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/QYZH.InteractiveMagazine.Infrastructure/Middleware/JwtAutoRefreshMiddleware.cs b/QYZH.InteractiveMagazine.Infrastructure/Middleware/JwtAutoRefreshMiddleware.cs index c3a79f2..eb1ef24 100644 --- a/QYZH.InteractiveMagazine.Infrastructure/Middleware/JwtAutoRefreshMiddleware.cs +++ b/QYZH.InteractiveMagazine.Infrastructure/Middleware/JwtAutoRefreshMiddleware.cs @@ -1,5 +1,6 @@ using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; +using QYZH.InteractiveMagazine.Infrastructure.Auth; using QYZH.InteractiveMagazine.Models.Settings; using System.IdentityModel.Tokens.Jwt; @@ -60,7 +61,11 @@ public class JwtAutoRefreshMiddleware { return; } - + var wxUserId = jwtToken.Claims.FirstOrDefault(c => c.Type == JwtHelper.WxUserIdClaimType)?.Value; + if (string.IsNullOrEmpty(wxUserId)) + { + return; + } var jwtSettings = _configuration.GetSection("JwtSettings").Get(); if (jwtSettings == null || jwtSettings.ExpiryMinutes <= 0) { @@ -68,7 +73,7 @@ public class JwtAutoRefreshMiddleware } // 每次请求都刷新 Redis 中 Token 的过期时间 - await RefreshRedisTokenExpiryAsync(userId, token, jwtSettings.ExpiryMinutes); + await RefreshRedisTokenExpiryAsync(wxUserId,userId, token, jwtSettings.ExpiryMinutes); } catch { @@ -79,7 +84,7 @@ public class JwtAutoRefreshMiddleware /// /// 刷新 Redis 中已存在 Token 的过期时间(保持会话活跃) /// - private async Task RefreshRedisTokenExpiryAsync(string userId, string currentToken, int expiryMinutes) + private async Task RefreshRedisTokenExpiryAsync(string wxUserId,string userId, string currentToken, int expiryMinutes) { // 检查 Admin Token var adminTokenKey = $"{AdminTokenKeyPrefix}:{userId}"; @@ -91,7 +96,7 @@ public class JwtAutoRefreshMiddleware } // 检查 WeChat Token - var wechatTokenKey = $"{WeChatTokenKeyPrefix}:{userId}"; + var wechatTokenKey = $"{WeChatTokenKeyPrefix}:{wxUserId}{userId}"; var wechatToken = await RedisHelper.GetAsync(wechatTokenKey); if (!string.IsNullOrEmpty(wechatToken)) { diff --git a/QYZH.InteractiveMagazine.Service/WeChatAuthService.cs b/QYZH.InteractiveMagazine.Service/WeChatAuthService.cs index 3693116..b70aa77 100644 --- a/QYZH.InteractiveMagazine.Service/WeChatAuthService.cs +++ b/QYZH.InteractiveMagazine.Service/WeChatAuthService.cs @@ -327,7 +327,7 @@ public class WeChatAuthService( var jwtSettings = GetJwtSettings(); var token = JwtHelper.GenerateToken(wxUser.Id, userId, userName, jwtSettings); - await RedisHelper.SetAsync($"{TokenKeyPrefix}:{userId}", token, TimeSpan.FromMinutes(jwtSettings.ExpiryMinutes)); + await RedisHelper.SetAsync($"{TokenKeyPrefix}:{wxUser.Id}{userId}", token, TimeSpan.FromMinutes(jwtSettings.ExpiryMinutes)); return new WeChatLoginOutput {