2013-08-14 40 views
17

ウェブトークンを署名と暗号化で保護する必要があります。だから、私はmakecert.exeで生成されたいくつかの証明書を使用していますJWTセキュリティトークンを暗号化する方法は?

var tokenHandler = new JwtSecurityTokenHandler(); 
var tokenDescriptor = new SecurityTokenDescriptor 
{ 
     Subject = new ClaimsIdentity(new[] 
     { 
      new Claim(ClaimTypes.Name, owner.Name), 
      new Claim(ClaimTypes.Role, owner.RoleClaimType), 
      new Claim("custom claim type", "custom content") 
     }), 
     TokenIssuerName = "self", 
     AppliesToAddress = "http://www.example.com", 
     Lifetime = new Lifetime(now, now.AddSeconds(60 * 3)), 
     EncryptingCredentials = new X509EncryptingCredentials(new X509Certificate2(cert)), 
     SigningCredentials = new X509SigningCredentials(cert1) 
}; 
var token = (JwtSecurityToken)tokenHandler.CreateToken(tokenDescriptor);    
var tokenString = tokenHandler.WriteToken(token); 

:私は、コードの次の行を書きました。 (私は、デバッガの下でtok変数でJSONを見ることができます)

var tokenHandlerDecr = new JwtSecurityTokenHandler(); 
var tok = tokenHandlerDecr.ReadToken(tokenString); 

とトークンコンテンツは暗号化されていません。そして、私は別のJwtSecurityTokenHandlerでトークン文字列を読み込みます。私は間違って何をしていますか?トークンデータを暗号化する方法

答えて

5

マイクロソフトのJWT実装では現在、暗号化(署名のみ)はサポートされていません。

+0

を、私は本当にいただければ幸いです。 –

+0

私はこのエクステンションを調べて、正しいと思われます。暗号化はまだサポートされていません。ありがとう! –

+0

これはまだですか? –

13

私はこれを古いポストだと知っていますが、誰かがまだ答えを探している場合に私の答えを追加しています。

この問題はMicrosoft.IdentityModel.Tokens version 5.1.3で解決されています。 CreateJwtSecurityToken関数には、トークンを暗号化するために暗号化証明書を受け入れるオーバーロードされたメソッドがあります。

受信者が署名を検証せずにJWTをそのまま読み込もうとすると、クレームは空です。次のコードスニペットです:

using Microsoft.IdentityModel.Tokens; 
using System.IdentityModel.Tokens.Jwt; 

const string sec = "ProEMLh5e_qnzdNUQrqdHPgp"; 
const string sec1 = "ProEMLh5e_qnzdNU"; 
var securityKey = new SymmetricSecurityKey(Encoding.Default.GetBytes(sec)); 
var securityKey1 = new SymmetricSecurityKey(Encoding.Default.GetBytes(sec1)); 

var signingCredentials = new SigningCredentials(
    securityKey, 
    SecurityAlgorithms.HmacSha512); 

List<Claim> claims = new List<Claim>() 
{ 
    new Claim("sub", "test"), 
}; 

var ep = new EncryptingCredentials(
    securityKey1, 
    SecurityAlgorithms.Aes128KW, 
    SecurityAlgorithms.Aes128CbcHmacSha256); 

var handler = new JwtSecurityTokenHandler(); 

var jwtSecurityToken = handler.CreateJwtSecurityToken(
    "issuer", 
    "Audience", 
    new ClaimsIdentity(claims), 
    DateTime.Now, 
    DateTime.Now.AddHours(1), 
    DateTime.Now, 
    signingCredentials, 
    ep); 


string tokenString = handler.WriteToken(jwtSecurityToken); 

// Id someone tries to view the JWT without validating/decrypting the token, 
// then no claims are retrieved and the token is safe guarded. 
var jwt = new JwtSecurityToken(tokenString); 

そして、ここではトークン復号化/検証するためのコードです:あなたはこのトピックに関するいくつかのリンクを私に提供することができれば、その場合

using Microsoft.IdentityModel.Tokens; 
using System.IdentityModel.Tokens.Jwt; 

const string sec = "ProEMLh5e_qnzdNUQrqdHPgp"; 
const string sec1 = "ProEMLh5e_qnzdNU"; 
var securityKey = new SymmetricSecurityKey(Encoding.Default.GetBytes(sec)); 
var securityKey1 = new SymmetricSecurityKey(Encoding.Default.GetBytes(sec1)); 

// This is the input JWT which we want to validate. 
string tokenString = string.Empty; 

// If we retrieve the token without decrypting the claims, we won't get any claims 
// DO not use this jwt variable 
var jwt = new JwtSecurityToken(tokenString); 

// Verification 
var tokenValidationParameters = new TokenValidationParameters() 
{ 
    ValidAudiences = new string[] 
    { 
     "536481524875-glk7nibpj1q9c4184d4n3gittrt8q3mn.apps.googleusercontent.com" 
    }, 
    ValidIssuers = new string[] 
    { 
     "https://accounts.google.com" 
    }, 
    IssuerSigningKey = securityKey, 
    // This is the decryption key 
    TokenDecryptionKey = securityKey1 
}; 

SecurityToken validatedToken; 
var handler = new JwtSecurityTokenHandler(); 

handler.ValidateToken(tokenString, tokenValidationParameters, out validatedToken); 
+0

トークンを検証するときにキーを渡すオプションがないので、どのように私はトークンを復号化しますか? –

+1

@SangSuantak TokenValidationParametersにはTokenDecryptionKeyプロパティがあります。 Validatorは内部的にこのプロパティを使用してトークンを復号化します。私は解読部を含めるように私の答えを更新しました – Amey

+0

更新していただきありがとうございます –

関連する問題