不透過認證取得 JWT payload

有個需求需取得 JWT 的 payload,但這個 token 是其他系統產出的,意味著我們無法對 JWT 做認證 (Authentication),沒辦法從 HttpContext.User 取得資訊

解決方法

IHttpContextAccessor 注入後,依下方的程式碼 decode,取得 token 內的 payload

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public readonly Info info = new Info();

public MyService(IHttpContextAccessor accessor)
{
    // 取得 token
    string token = accessor.HttpContext.Request.Headers["Authorization"].ToString();
    token = token.Replace("Bearer ", string.Empty);
    
    if (!string.IsNullOrWhiteSpace(token))
    {
        // 取得 payload
        var handler = new JwtSecurityTokenHandler();
        var jwtToken = handler.ReadToken(token) as JwtSecurityToken;
        var payload = JsonSerializer.Serialize(jwtToken.Claims.ToDictionary(k => k.Type, v => v.Value));
        
        // 轉成你要的類別
        Info = JsonSerializer.Deserialize<Info>(payload, new JsonSerializerOptions
        {
            PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
        });
    }
}

相關連結