2017-09-13 13 views
0

次のコードでJWTを生成し検証しています。.netのJWT生成と検証で "Key is not supported"がスローされます

static string GenerateToken() 
{ 
    var tokenHandler = new JwtSecurityTokenHandler(); 
    var certificate = new X509Certificate2(@"Test.pfx", "123"); 
    var rsa = certificate.GetRSAPrivateKey(); 

    var tokenDescriptor = new SecurityTokenDescriptor 
    { 
     Subject = new ClaimsIdentity(), 
     Issuer = "Self", 
     IssuedAt = DateTime.Now, 
     Audience = "Others", 
     Expires = DateTime.MaxValue, 
     SigningCredentials = new SigningCredentials(
      new RsaSecurityKey(rsa), 
      SecurityAlgorithms.RsaSha256Signature) 
    }; 

    var token = tokenHandler.CreateToken(tokenDescriptor); 
    return tokenHandler.WriteToken(token); 
} 

static bool ValidateToken(string token) 
{ 
    var tokenHandler = new JwtSecurityTokenHandler(); 
    var certificate = new X509Certificate2(@"Test.cer"); 
    var rsa = certificate.GetRSAPublicKey(); 

    var validationParameters = new TokenValidationParameters 
    { 
     ValidAudience = "Others", 
     ValidIssuer = "Self", 
     IssuerSigningKey = new RsaSecurityKey(rsa) 
    }; 

    var principal = tokenHandler.ValidateToken(token, validationParameters, out SecurityToken securityToken); 
    if (principal == null) 
     return false; 
    if (securityToken == null) 
     return false; 

    return true; 
} 

このコードは、.net標準2.0とnet46をターゲットとするライブラリにあります。

.netコアアプリ2.0プロジェクトでライブラリを使用すると、すべてが期待通りに機能しています。私は以下のナゲットパッケージを使用します。

  • System.IdentityModel.Tokens.Jwt => 5.1.4
  • System.Security.Cryptography.Csp => 4.3.0

しかし、私は.net46 Iと同じコードをビルドするときトークンを生成しようとすると、次の例外が発生します。

var token = tokenHandler.CreateToken(tokenDescriptor); 

System.NotSupportedException: 'NotSupported_Method'

私はトークンを検証しようとすると、次の例外がスローされます。

var principal = tokenHandler.ValidateToken(token, validationParameters, out SecurityToken securityToken); 

Microsoft.IdentityModel.Tokens.SecurityTokenInvalidSignatureException:「IDX10503:署名の検証に失敗しました。試したキー: 'Microsoft.IdentityModel.Tokens.RsaSecurityKey、KeyId: '。

答えて

1

RsaSecurityKeyを使用する代わりに、私は今すぐX509SecurityKeyを使用します。これは、netstandard2.0とnet46の両方で機能します。

static string GenerateToken() 
{ 
    var tokenHandler = new JwtSecurityTokenHandler(); 
    var certificate = new X509Certificate2(@"Test.pfx", "123"); 
    var securityKey = new X509SecurityKey(certificate); 

    var tokenDescriptor = new SecurityTokenDescriptor 
    { 
     Subject = new ClaimsIdentity(), 
     Issuer = "Self", 
     IssuedAt = DateTime.Now, 
     Audience = "Others", 
     Expires = DateTime.MaxValue, 
     SigningCredentials = new SigningCredentials(
      securityKey, 
      SecurityAlgorithms.RsaSha256Signature) 
    }; 

    var token = tokenHandler.CreateToken(tokenDescriptor); 
    return tokenHandler.WriteToken(token); 
} 

static bool ValidateToken(string token) 
{ 
    var tokenHandler = new JwtSecurityTokenHandler(); 
    var certificate = new X509Certificate2(@"Test.cer"); 
    var securityKey = new X509SecurityKey(certificate); 

    var validationParameters = new TokenValidationParameters 
    { 
     ValidAudience = "Others", 
     ValidIssuer = "Self", 
     IssuerSigningKey = securityKey 
    }; 

    var principal = tokenHandler.ValidateToken(token, validationParameters, out SecurityToken securityToken); 
    if (principal == null) 
     return false; 
    if (securityToken == null) 
     return false; 

    return true; 
} 

はまた、私は唯一のSystem.IdentityModel.Tokens.Jwt nugetパッケージを必要とSystem.Security.Cryptography.Cspパッケージを削除することができます。

関連する問題