2013-08-14 13 views
17

मुझे साइन-इन और एन्क्रिप्शन के साथ अपना वेब-टोकन सुरक्षित करने की आवश्यकता है। तो, मैं कुछ प्रमाण पत्र, makecert.exe साथ उत्पन्न उपयोग कर रहा हूँजेडब्ल्यूटी सुरक्षा टोकन को एन्क्रिप्ट कैसे करें?

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); 

: मैं कोड की अगली पंक्तियों में लिखा था। तब मैं एक और JwtSecurityTokenHandler साथ टोकन स्ट्रिंग पढ़ें:

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

और टोकन सामग्री एन्क्रिप्टेड नहीं है (मैं डिबगर के तहत tok चर में json देख सकते हैं)। मैं क्या गलत कर रहा हूं? टोकन डेटा एन्क्रिप्ट कैसे करें?

उत्तर

5

मेरी समझ यह है कि माइक्रोसॉफ्ट के जेडब्ल्यूटी कार्यान्वयन वर्तमान में एन्क्रिप्शन (केवल हस्ताक्षर) का समर्थन नहीं करता है।

+0

यदि हां, तो मैं सच में अगर तुम मुझे इस विषय पर कुछ लिंक के साथ प्रदान कर सकता है की सराहना करेंगे। –

+0

मैंने इस एक्सटेंशन की खोज की है और ऐसा लगता है कि आप सही हैं - एन्क्रिप्शन अभी तक समर्थित नहीं है। धन्यवाद! –

+0

क्या यह अभी भी मामला है? –

13

मुझे यह एक पुरानी पोस्ट पता है, लेकिन अगर कोई अभी भी उत्तर खोज रहा है तो मैं अपना जवाब जोड़ रहा हूं।

यह समस्या Microsoft.IdentityModel.Tokens version 5.1.3 में संबोधित है। CreateJwtSecurityToken फ़ंक्शन में एक अधिभारित विधि उपलब्ध है जो टोकन को एन्क्रिप्ट करने के लिए एन्क्रिप्टिंग प्रमाण-पत्र स्वीकार करता है।

यदि रिसीवर हस्ताक्षर को मान्य नहीं करता है और जेडब्ल्यूटी को पढ़ने की कोशिश करता है तो दावे खाली होते हैं। पीछा कर रहा है कोड का टुकड़ा:

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 टोकन वैलिडेशन पैरामीटर में टोकनडेक्रिप्शनकी संपत्ति है। वैलिडेटर टोकन को डिक्रिप्ट करने के लिए आंतरिक रूप से इस प्रॉपर्टी का उपयोग करता है। मैंने डिक्रिप्शन भाग – Amey

+0

अपडेट के लिए धन्यवाद शामिल करने के लिए अपना जवाब अपडेट किया है –

संबंधित मुद्दे