【问题标题】:Unable to validate JWT Token无法验证 JWT 令牌
【发布时间】:2019-07-29 06:16:19
【问题描述】:

我使用下面的代码来生成 Token String。

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” P>

验证时出现以下错误。

任何帮助将不胜感激。

【问题讨论】:

    标签: .net .net-core jwt


    【解决方案1】:

    “eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9.eyJzb21lICI6ImhlbGxvICIsInNjb3BlIjoiaHR0cDovL2R1bW15LmNvbS8ifQ.FPkHESpldjwEsdE_ii8936gFq4pfptl3b6ao13BTLZk” P>

    令牌缺少到期字段。这不是 .dot net 的强制早期版本。来自 .dot 核心和最新版本的 JWT 令牌验证器,这是必需的。

    【讨论】:

      猜你喜欢
      • 2017-07-31
      • 2019-06-23
      • 2022-01-18
      • 2017-09-18
      • 2020-05-11
      • 2017-07-27
      • 2019-10-20
      • 2016-01-17
      • 2017-04-05
      相关资源
      最近更新 更多