繁体   English   中英

如何从.NET / C#代码验证Apigee Edge生成的JWT令牌?

[英]How to Validate Apigee Edge generated JWT Token from .NET/C# code?

我已经在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:',InternalId:'96edcecb-17ad-4022-a50b-558f426ed337'。 ,KeyId:“。 捕获到异常:'System.ArgumentOutOfRangeException:IDX10603:解密失败。 尝试的键:“ HS256”。 捕获到异常:“ 128”。 令牌:“ 96”。参数名称:Microsoft.IdentityModel.Tokens.CryptoProviderFactory.CreateSignatureProvider(Microsoft.IdentityModel.Tokens.SymmetricSignatureProvider..ctor(安全密钥,字符串算法,布尔值WillCreateSignatures)处的KeySize(安全密钥,字符串算法,布尔值WillCreateSignatures)在Microsoft.IdentityModel.Tokens.CryptoProviderFactory.CreateForVerifying(SecurityKey key,String algorithm)在System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateSignature(Byte [] encodingBytes,Byte [] signature,SecurityKey key,String Algorithm,TokenValidationParameters validateParameters) System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateSignature(字符串令牌,TokenValidationParametersvalidationParameters)'.........

您的密钥大小不够长,就像异常中提到的那样:

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

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

IssuerSigningKey =新的SymmetricSecurityKey(Encoding.UTF8.GetBytes(“ key”))

在上面的代码中,“键”值的大小小于预期值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM