簡體   English   中英

創建JWT匹配.Net core生成的JWT

[英]Creating JWT to match the JWT generated by .Net core

與我合作的第三方 API 要求我在客戶端生成 JWT。 我了解到他們使用以下代碼來驗證 JWT。

using System;
using System.Text;
using Microsoft.IdentityModel.Tokens;
using Microsoft.AspNetCore.Builder;

// The key length needs to be of sufficient length, or otherwise an error will occur.
var tokenSecretKey = Encoding.UTF8.GetBytes(Configuration["TokenSecretKey"]);

var tokenValidationParameters = new TokenValidationParameters
{
    // Token signature will be verified using a private key.
    ValidateIssuerSigningKey = true,
    IssuerSigningKey = new SymmetricSecurityKey(tokenSecretKey),
    ValidateIssuer = false,
    ValidateAudience = false
};

services.AddAuthentication(options =>
        {
            options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        }).AddJwtBearer(options => { options.RequireHttpsMetaData = false;
options.SaveToken = true; 
 options.TokenValidationParameters = tokenValidationParameters; 
});

在客戶端,我使用 Jose JWT 創建的令牌生成如下。只有這似乎適用於我正在使用的 .Net 4.0 框架。

return Jose.JWT.Encode(claims, byteArrayOfKey, Jose.JwsAlgorithm.HS256);

但驗證失敗,服務器端出現 401。 有什么我可以做的匹配服務器端。

檢查客戶端和服務器使用的算法是否相同。 基本上,不要忽視發行者,任何人都可以偽造服務器,因此最佳實踐是使用發行者作為強制驗證。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM