无法使用JwtSecurityTokenHandler()。ValidateToken()读取CyrptoJS生成的JWT

时间:2019-03-01 22:02:02

标签: javascript c# jwt

我正在使用CyrptoJS生成令牌,该令牌以jwt.io测试形式正确编码和解码。使用HMACSha256。

该过程引发异常:

IDX12709: CanReadToken() returned false. JWT is not well formed: '[PII is hidden]'.
The token needs to be in JWS or JWE Compact Serialization Format. 
(JWS): 'EncodedHeader.EndcodedPayload.EncodedSignature'. 
(JWE): 'EncodedProtectedHeader.EncodedEncryptedKey.EncodedInitializationVector.EncodedCiphertext.EncodedAuthenticationTag'.

在api中收到的令牌为: eyAiYWxnIjogIkhTMjU2IiwgInR5cGUiOiJKV1QifQ == ..

我用来验证令牌的代码是:

public static Boolean ValidateToken(string jwtToken,  string key)
    {
        var securityKey = new SymmetricSecurityKey(Convert.FromBase64String(key));
        var validationParameters = new TokenValidationParameters()
        {
            ValidateIssuerSigningKey = true,
            IssuerSigningKey = securityKey
        };
        SecurityToken validatedToken;
        var claimPrincipal = new JwtSecurityTokenHandler().ValidateToken(jwtToken, validationParameters, out validatedToken);

        return validatedToken.ValidFrom <= DateTime.Now; 
    }

1 个答案:

答案 0 :(得分:0)

问题出在CryptoJS的编码中,这有助于我进行分类https://www.jonathan-petitcolas.com/2014/11/27/creating-json-web-token-in-javascript.html,而我没有对值进行url编码。删除=