fix RedisHelper wxTokenKeyPrefix

This commit is contained in:
glz
2026-06-29 17:49:28 +08:00
parent 6b09006071
commit 2fcff63ee2
2 changed files with 10 additions and 5 deletions

View File

@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using QYZH.InteractiveMagazine.Infrastructure.Auth;
using QYZH.InteractiveMagazine.Models.Settings; using QYZH.InteractiveMagazine.Models.Settings;
using System.IdentityModel.Tokens.Jwt; using System.IdentityModel.Tokens.Jwt;
@ -60,7 +61,11 @@ public class JwtAutoRefreshMiddleware
{ {
return; return;
} }
var wxUserId = jwtToken.Claims.FirstOrDefault(c => c.Type == JwtHelper.WxUserIdClaimType)?.Value;
if (string.IsNullOrEmpty(wxUserId))
{
return;
}
var jwtSettings = _configuration.GetSection("JwtSettings").Get<JwtSettings>(); var jwtSettings = _configuration.GetSection("JwtSettings").Get<JwtSettings>();
if (jwtSettings == null || jwtSettings.ExpiryMinutes <= 0) if (jwtSettings == null || jwtSettings.ExpiryMinutes <= 0)
{ {
@ -68,7 +73,7 @@ public class JwtAutoRefreshMiddleware
} }
// 每次请求都刷新 Redis 中 Token 的过期时间 // 每次请求都刷新 Redis 中 Token 的过期时间
await RefreshRedisTokenExpiryAsync(userId, token, jwtSettings.ExpiryMinutes); await RefreshRedisTokenExpiryAsync(wxUserId,userId, token, jwtSettings.ExpiryMinutes);
} }
catch catch
{ {
@ -79,7 +84,7 @@ public class JwtAutoRefreshMiddleware
/// <summary> /// <summary>
/// 刷新 Redis 中已存在 Token 的过期时间(保持会话活跃) /// 刷新 Redis 中已存在 Token 的过期时间(保持会话活跃)
/// </summary> /// </summary>
private async Task RefreshRedisTokenExpiryAsync(string userId, string currentToken, int expiryMinutes) private async Task RefreshRedisTokenExpiryAsync(string wxUserId,string userId, string currentToken, int expiryMinutes)
{ {
// 检查 Admin Token // 检查 Admin Token
var adminTokenKey = $"{AdminTokenKeyPrefix}:{userId}"; var adminTokenKey = $"{AdminTokenKeyPrefix}:{userId}";
@ -91,7 +96,7 @@ public class JwtAutoRefreshMiddleware
} }
// 检查 WeChat Token // 检查 WeChat Token
var wechatTokenKey = $"{WeChatTokenKeyPrefix}:{userId}"; var wechatTokenKey = $"{WeChatTokenKeyPrefix}:{wxUserId}{userId}";
var wechatToken = await RedisHelper.GetAsync(wechatTokenKey); var wechatToken = await RedisHelper.GetAsync(wechatTokenKey);
if (!string.IsNullOrEmpty(wechatToken)) if (!string.IsNullOrEmpty(wechatToken))
{ {

View File

@ -327,7 +327,7 @@ public class WeChatAuthService(
var jwtSettings = GetJwtSettings(); var jwtSettings = GetJwtSettings();
var token = JwtHelper.GenerateToken(wxUser.Id, userId, userName, jwtSettings); 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 return new WeChatLoginOutput
{ {