【问题标题】:How to Validate Apigee Edge generated JWT Token from .NET/C# code?如何验证 Apigee Edge 从 .NET/C# 代码生成的 JWT 令牌?
【发布时间】:2019-12-14 16:03:28
【问题描述】:

​我在 Apigee Edge 中创建了一个代理来生成 JWT 令牌。 我在 Apigee Edge 中创建了另一个代理验证 JWT 令牌,我可以使用它进行验证。 现在我无法完全从 .NET/C# 代码验证 JWT 令牌。

下面是我试过的 .NET 代码:

private static bool ValidateToken(string authToken, string key)
{
    var tokenHandler = new JwtSecurityTokenHandler();
    var validationParameters = GetValidationParameters(key);
    SecurityToken validatedToken;
    IPrincipal principal = tokenHandler.ValidateToken(authToken, validationParameters, out validatedToken);
    return true;
}

private static TokenValidationParameters GetValidationParameters(string key)
{
    return new TokenValidationParameters()
    {
        ValidateLifetime = false, // Because there is no expiration in the generated token
        ValidateAudience = false, // Because there is no audiance in the generated token
        ValidateIssuer = false,   // Because there is no issuer in the generated token
        ValidIssuer = "urn:apigee-edge-JWT-policy-test",
        ValidAudience = "audience1",
        IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("key")) // The same key as the one that generate the token
    };
}

还有来自 Apigee Edge 的 JWT 生成策略代码:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<GenerateJWT async="false" continueOnError="false" enabled="true" name="Generate-JWT-1">
    <DisplayName>Generate JWT-1</DisplayName>
    <Algorithm>HS256</Algorithm>
    <SecretKey>
        <Value ref="private.key"/>
    </SecretKey>
    <Subject>subject-subject</Subject>
    <Issuer>urn://apigee-edge-JWT-policy-test</Issuer>
    <Audience>audience1,audience2</Audience>
    <ExpiresIn>8h</ExpiresIn>
    <AdditionalClaims>
        <Claim name="userId" type="string" ref="request.formparam.username"/>
    </AdditionalClaims>
    <OutputVariable>jwt-variable</OutputVariable>
</GenerateJWT>

这是错误信息:

Microsoft.IdentityModel.Tokens.SecurityTokenInvalidSignatureException: IDX10503:签名验证失败。尝试过的键: 'Microsoft.IdentityModel.Tokens.SymmetricSecurityKey, KeyId: '',内部标识: '96edcecb-17ad-4022-a50b-558f426ed337'。 ,密钥ID:'。 捕获的异常:'System.ArgumentOutOfRangeException:IDX10603: 解密失败。尝试过的键:'HS256'。捕获的异常: '128'。令牌:'96' 参数名称:KeySize at Microsoft.IdentityModel.Tokens.SymmetricSignatureProvider..ctor(SecurityKey 键,字符串算法,布尔 willCreateSignatures)在 Microsoft.IdentityModel.Tokens.CryptoProviderFactory.CreateSignatureProvider(SecurityKey 键,字符串算法,布尔 willCreateSignatures)在 Microsoft.IdentityModel.Tokens.CryptoProviderFactory.CreateForVerifying(安全密钥 键,字符串算法)在 System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateSignature(字节[] encodedBytes、Byte[] 签名、SecurityKey 密钥、String 算法、 TokenValidationParameters 验证参数)在 System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateSignature(字符串 token, TokenValidationParameters validationParameters) '.......

【问题讨论】:

  • 你能展示一个生成的jwt的例子吗?
  • @ cyptus这:eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJzdWJqZWN0LXN1YmplY3QiLCJhdWQiOlsiYXVkaWVuY2UxIiwiYXVkaWVuY2UyIl0sImlzcyI6InVybjpcL1wvYXBpZ2VlLWVkZ2UtSldULXBvbGljeS10ZXN0IiwiZXhwIjoxNTY1MTgwNDA5LCJ1c2VySWQiOiIxOTgxMzciLCJpYXQiOjE1NjUxNTE2MDksImp0aSI6IjE4OWY1NzBkLTNlMzQtNDRiMS04NWI3LWRmYzBiMTFmZjk3YyJ9.FiGy7tRbyGg4TOe-hczU2utph5ksmtXu-fsOa6dodXQ 跨度>
  • 似乎没问题,我只看到您的发行人与令牌中的那个不匹配(缺少“//”)。
  • 我认为您的密钥不够长:捕获的异常:'128'。令牌:'96' 参数名称:KeySize - 你可以尝试更长的密钥吗?
  • 感谢您的意见@cyptus。关键大小是问题。非常感谢!

标签: c# .net jwt apigee


【解决方案1】:

您的密钥大小不像例外提到的那样长:

Exceptions caught: '128'. token: '96' Parameter name: KeySize

使用与例外大小匹配的更长的密钥

【讨论】:

    【解决方案2】:

    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("key"))

    在上面的代码中,“key”值的大小比预期的要小。

    【讨论】:

      猜你喜欢
      • 2021-09-01
      • 2017-06-22
      • 2021-06-27
      • 2021-08-21
      • 2018-06-12
      • 2021-05-21
      • 2022-11-21
      • 2018-10-16
      • 2020-05-08
      相关资源
      最近更新 更多