From 599aeb0d380f9b194e1dc363364779f3b498969e Mon Sep 17 00:00:00 2001
From: llj <132905093+newwwwwwtree@users.noreply.github.com>
Date: 星期四, 30 四月 2026 10:58:03 +0800
Subject: [PATCH] 1

---
 WebAPI/Service/JwtAuthorizeAttribute.cs |   28 ++++++++++++++++++++++++++++
 1 files changed, 28 insertions(+), 0 deletions(-)

diff --git a/WebAPI/Service/JwtAuthorizeAttribute.cs b/WebAPI/Service/JwtAuthorizeAttribute.cs
index 3ef48a0..9418f11 100644
--- a/WebAPI/Service/JwtAuthorizeAttribute.cs
+++ b/WebAPI/Service/JwtAuthorizeAttribute.cs
@@ -6,6 +6,7 @@
 using System.Security.Claims;
 using System.Threading;
 using System.Threading.Tasks;
+using System.Web;
 using System.Web.Http;
 using System.Web.Http.Controllers;
 using System.Web.Http.Filters;
@@ -53,6 +54,20 @@
                 return;
             }
 
+            // 妫�鏌� Token鍜屽綋鍓嶇櫥褰曠敤鎴锋槸鍚﹀尮閰�
+            var HUserName = GetUserNameFromHeader(actionContext) ?? "";
+            var TokenUserName = JWTHelper.getUserName(token);
+            if(!string.IsNullOrWhiteSpace(HUserName))
+            {
+                HUserName = HttpUtility.UrlDecode(HUserName);
+            }
+            if(TokenUserName != HUserName)
+            {
+                // 濡傛灉Token鍜屽綋鍓嶇櫥褰曠敤鎴蜂笉鍖归厤锛屽垯闇�瑕佸憡鐭ュ墠绔敤鎴� Token 杩囨湡锛岄渶閲嶆柊鐧诲綍
+                HandleUnauthorized(actionContext);
+                return;
+            }
+            
             // 璁剧疆鐢ㄦ埛
             actionContext.RequestContext.Principal = principal;
 
@@ -146,6 +161,19 @@
         return null;
     }
 
+    // 浠嶩eader涓幏鍙栫敤鎴峰悕
+    private string GetUserNameFromHeader(HttpActionContext context)
+    {
+        var headers = context.Request.Headers;
+        string userName = "";
+        if (headers.Contains("X-Username"))
+        {
+            userName = headers.GetValues("X-Username").FirstOrDefault();
+        }
+
+        return userName;
+    }
+
     // 401 鏈巿鏉�
     private void HandleUnauthorized(HttpActionContext context)
     {

--
Gitblit v1.9.1