无法验证JWT令牌

时间:2019-03-07 11:13:33

标签: .net .net-core jwt

Am使用以下代码生成令牌字符串。

string key = "401b09eab3c013d4ca54922bb802bec8fd5318192b0a75f201d8b3727429090fb337591abd3e44453b954555b7a0812e1081c39b740293f765eae731f5a65ed1";

            //// Create Security key  using private key above:
            //// not that latest version of JWT using Microsoft namespace instead of System
            var securityKey = new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(Encoding.UTF8.GetBytes(key));

            ////Also note that securityKey length should be >256b
            ////so you have to make sure that your private key has a proper length
            ////
            var credentials = new Microsoft.IdentityModel.Tokens.SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256Signature);

            ////  Finally create a Token
            var header = new JwtHeader(credentials);

            ////Some PayLoad that contain information about the  customer
            var payload = new JwtPayload
               {
                   {
                    "some ", "hello "
                },
                   {
                    "scope", "http://dummy.com/"
                },
               };


            var secToken = new JwtSecurityToken(header, payload);

            var tokenString1 = handler.WriteToken(secToken);

            Console.WriteLine(tokenString);
            Console.WriteLine("Consume Token");



            var token = handler.ReadJwtToken(tokenString);

现在,当我尝试使用以下代码验证令牌时,出现错误:

// Just to validate the authenticity of the certificate. 
        var tokenValidationParameters = new TokenValidationParameters
        {


            ValidateIssuer = false,
            ValidateAudience = false,
            ValidateLifetime = false,
            ValidateIssuerSigningKey = false,

            IssuerSigningKeys = GetEmbeddedKeys(jwtSecurityToken)
        };

        // Perform the validation 
        var tokenHandler = new JwtSecurityTokenHandler();
        SecurityToken validatedToken;
        try
        {
            tokenHandler.ValidateToken(jwtTokenRequest.ClientJwtTokenString, tokenValidationParameters, out validatedToken);
        }
        catch (ArgumentException)
        {
            throw EnumException.Create(LicenseClientJwtError.FailedToValidateJwtTokenSignature, string.Format(CultureInfo.InvariantCulture, "PostParseJwtToken - Failed to validate JWT Token Signature. The Token does not have 3 or 5 parts {0}", jwtTokenRequest.ClientJwtTokenString));
        }

private static X509SecurityKey[] GetEmbeddedKeys(JwtSecurityToken token)
    {

        X509SecurityKey[] keys = null;
        if (token.Header.TryGetValue("x5c", out var certificateAsString))
        {
            keys = (certificateAsString as JArray).Values<string>().Select(x => new X509SecurityKey(new X509Certificate2(Convert.FromBase64String(x)))).ToArray();
            return keys;
        }

        return null;
    }

正在逐渐jwtTokenRequest.ClientJwtTokenString = “eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9.eyJzb21lICI6ImhlbGxvICIsInNjb3BlIjoiaHR0cDovL2R1bW15LmNvbS8ifQ.FPkHESpldjwEsdE_ii8936gFq4pfptl3b6ao13BTLZk”

验证时出现以下错误。 Error

任何帮助都会令人感激。

1 个答案:

答案 0 :(得分:1)

“eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9.eyJzb21lICI6ImhlbGxvICIsInNjb3BlIjoiaHR0cDovL2R1bW15LmNvbS8ifQ.FPkHESpldjwEsdE_ii8936gFq4pfptl3b6ao13BTLZk”

令牌缺少到期字段。那不是.dot net的强制性早期版本。从.dot核心和最新版本的JWT令牌验证器开始。