2011-01-17 14 views
5

Implementiamo STS (autenticazione basata su attestazioni) per i servizi basati su REST. Uno dei motivi per cui molti decidiamo di creare servizi REST (con JSON) è stato il piccolo ingombro sul filo. Con STS, il token SAML con poche affermazioni, la dimensione SAML diventa pochi K byte. Per la maggior parte delle chiamate REST in cui non viene restituito l'elenco degli oggetti, la dimensione della risposta è di 100 secondi e per quelle chiamate questo token sembra troppo sovraccarico. Come hai affrontato questo nei tuoi progetti?Dimensioni token SAML e REST

risposta

0

È possibile utilizzare token SAML con endpoint REST, ma più spesso si trovano persone che utilizzano semplicemente token Web (SWT). Più piccolo, più semplice, ecc.

ACS (Servizio di controllo accessi in Windows Azure PLatform) implementa questo, ad esempio.

+0

Non sembra che lo SWT proceda in avanti. JWT è anche nell'infanzia. Sembra che per ora abbiamo a che fare con SAML. http://startersts.codeplex.com/discussions/242113?ProjectName=startersts –

2

... o JWT (Token Web JSon). ACS supporta anche questi. Controllare questo articolo: JSON Web Token Handler for the Microsoft .NET Framework 4.5 Ecco un esempio di utilizzo di questa libreria con .Net 4.5 che emette e convalida un JWT firmato con HMAC SHA256 basato su chiave simmetrica.

string jwtIssuer = "MyIssuer"; 
string jwtAudience = "MyAudience"; 

// Generate symmetric key for HMAC-SHA256 signature 
RNGCryptoServiceProvider cryptoProvider = new RNGCryptoServiceProvider(); 
byte[] keyForHmacSha256 = new byte[64]; 
cryptoProvider.GetNonZeroBytes(keyForHmacSha256); 

/////////////////////////////////////////////////////////////////// 
// Create signing credentials for the signed JWT. 
// This object is used to cryptographically sign the JWT by the issuer. 
SigningCredentials sc = new SigningCredentials(
           new InMemorySymmetricSecurityKey(keyForHmacSha256), 
           "http://www.w3.org/2001/04/xmldsig-more#hmac-sha256", 
           "http://www.w3.org/2001/04/xmlenc#sha256"); 

/////////////////////////////////////////////////////////////////// 
// Create token validation parameters for the signed JWT 
// This object will be used to verify the cryptographic signature of the received JWT 
TokenValidationParameters validationParams = 
    new TokenValidationParameters() 
    { 
     AllowedAudience = s_jwtAudience, 
     ValidIssuer = s_jwtIssuer, 
     ValidateExpiration = true, 
     ValidateNotBefore = true, 
     ValidateIssuer = true, 
     ValidateSignature = true, 
     SigningToken = new BinarySecretSecurityToken(keyForHmacSha256), 
    }; 

/////////////////////////////////////////////////////////////////// 
// Create JWT handler 
// This object is used to write/sign/decode/validate JWTs 
JWTSecurityTokenHandler jwtHandler = new JWTSecurityTokenHandler(); 

// Create a simple JWT claim set 
IList<Claim> payloadClaims = new List<Claim>() { new Claim("clm1", "clm1 value"), }; 

// Create a JWT with signing credentials and lifetime of 12 hours 
JWTSecurityToken jwt = 
    new JWTSecurityToken(jwtIssuer, jwtAudience, payloadClaims, sc, DateTime.UtcNow, DateTime.UtcNow.AddHours(12.0)); 

// Serialize the JWT 
// This is how our JWT looks on the wire: <Base64UrlEncoded header>.<Base64UrlEncoded body>.<signature> 
string jwtOnTheWire = jwtHandler.WriteToken(jwt); 

// Validate the token signature (we provide the shared symmetric key in `validationParams`) 
// This will throw if the signature does not validate 
jwtHandler.ValidateToken(jwtOnTheWire, validationParams); 

// Parse JWT from the Base64UrlEncoded wire form (<Base64UrlEncoded header>.<Base64UrlEncoded body>.<signature>) 
JWTSecurityToken parsedJwt = jwtHandler.ReadToken(jwtOnTheWire) as JWTSecurityToken; 
+0

Non riesco a trovare alcuna possibilità di utilizzare una chiave simmetrica, con quella. Tipo bloccato :-( –

+1

Non sono sicuro di aver capito cosa intendi, ma, nel caso, ho aggiornato la mia risposta sopra per mostrare una parte di codice che crea, firma, serializza una JWT e poi esegue il processo inverso. – Kastorskij

+0

Sì ! Perfetto proprio quello di cui avevo bisogno. Grazie !!! –

Problemi correlati