2014-07-09 20 views
10

Sto tentando di implementare la specifica OpenID Connect per la mia organizzazione. Sto usando l'implementazione OWIN di Microsoft di OpenID Connect in un'applicazione di test relying party per verificare la mia implementazione del protocollo.SecurityTokenSignatureKeyNotFoundException durante la convalida della firma JWT

ho esposto il seguente documento di metadati:

{ 
    "issuer": "https://acs.contoso.com/", 
    "authorization_endpoint": "http://localhost:53615/oauth2/auth", 
    "token_endpoint": "http://localhost:53615/oauth2/token", 
    "userinfo_endpoint": "http://localhost:53615/connect/userinfo", 
    "jwks_uri": "http://localhost:53615/connect/keys", 
    "ui_locales_supported": [ 
    "en-GB" 
    ] 
} 

La chiave di firma è esposto come questo documento:

{ 
    "keys": [ 
    { 
     "n": "xpXxl3M-YkZlzQJdArO1TfOGT2no-UL4dbZ7WuSCNIsSfyGDaqUXjMMHNyq9yD3vp-NCyk8kmn7d5XqHufnceXJM8q4xTrhN3lvywdBSbR-dwXsA-B-MJVgfiK0d_z-mxP9ew2Hj9-KkWbWCzsswlWp3gZ4mB4RGutB1IRSzXVIbvZ-MtKUb6XUDU4LDb_c1xCEXWZxhR-o1a1dLfObH2hHJ-w5y6odGlKtOFx4i4h0u7-Oj5R6k5b2YXEHM0IuYeN0u0sQvrTecokntGzPrvhnKy69I7Z_az5rC5kgloh25D9lTbe4vcRU7FXlYCFYDZsT0_IkGIXRi7brOS4f1ow", 
     "e": "AQAB", 
     "kty": "RSA", 
     "use": "sig", 
     "alg": "RS256", 
     "kid": "F8A59280B3D13777CC7541B3218480984F421450" 
    } 
    ] 
} 

Il token di identità viene generato utilizzando la classe JwtSecurityToken e il suo gestore associato , utilizzando la classe X509SigningCredentials. Questo codice è rappresentativo di come il token viene costruito e restituito al sistema chiamante come parametro dei dati di risposta.

var credentials = new X509SigningCredentials(cert); // My certificate. 
var issuedTime = DateTime.UtcNow; 
var expiresTime = issuedTime.AddMinutes(5); 
var epoch = new DateTime(1970, 01, 01, 0, 0, 0); 

var claims = new[] 
{ 
    new Claim("sub", Guid.NewGuid().ToString()), 
    new Claim("iat" Math.Floor((issuedTime - epoch).TotalSeconds).ToString()), 
    new Claim("nonce", nonce), // Value from client 
} 

var token = new JwtSecurityToken(
    "https://acs.contoso.com", 
    client_id, // Value from client 
    claims, 
    new Lifetime(issuedTime, expiresTime), 
    credentials); 

var handler = new JwtSecurityTokenHandler(); 
parameters.Add("id_token", handler.WriteToken(token)); // Outgoing parameters. 

Quando si tenta di passare il sottoscritto segnalino indietro all'applicazione relying party, il middleware OWIN accetta il POST e cerca di verificare la firma del token. In tal modo, la seguente eccezione viene generata:

SecurityTokenSignatureKeyNotFoundException: IDX10500: Firma convalida non è riuscita. Impossibile risolvere SecurityKeyIdentifier: 'SecurityKeyIdentifier (IsReadOnly = False, Count = 1, la clausola [0] = X509ThumbprintKeyIdentifierClause (Hash = 0xF8A59280B3D13777CC7541B3218480984F421450))', token: '{ "tip": "JWT", "alg": "RS256", "x5t": "- KWSgLPRN3fMdUGzIYSAmE9CFFA"}. {"Iss": "https://test.accesscontrol.net/", "aud": "test", "nbf": 1404917162, "exp": 1404917462, "sub": "60eb55ec- 0699-4068-bfa6-41666fc2b2e9" , "iat": "1.404.917,162 mila"} RawData: eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Ii1LV1NnTFBSTjNmTWRVR3pJWVNBbUU5Q0ZGQSJ9.eyJpc3MiOiJodHRwczovL2Fjcy5zdXJlY2xvdWQuY29tLyIsImF1ZCI6InRlc3QiLCJuYmYiOjE0MDQ5MTcxNjIsImV4cCI6MTQwNDkxNzQ2Miwic3ViIjoiNjBlYjU1ZWMtMDY5OS00MDY4LWJmYTYtNDE2NjZmYzJiMmU5IiwiaWF0IjoiMTQwNDkxNzE2MiJ9.xkP0RwlX3CYfU0KhFsVvLJC94WK22DTqNTm71cfjiJ8VUHv3b2YhDqfq70N8mQEyiR8vTR6OQqnO6UqXqX4RXUs6ZkfK 9Liv3n9NhCs97wJhP2jfefJYeScYtRmWcNNWSSL7vkm2JXQfwKOQTnOGp-ba04TtI6jVrjhOQXH43eCJ9vNuBUzdD-t8CAdmnbvH0nWpIB8kWbw5v8Sa0aQuxMjJYbLC_2Iw3X13dqnyVjp4fA7eSB8N7c1it0KEB-VKfUqiGD3VecyEZGGZbaGE8rvVet5QrY1lJ3V4yM8j6-xDc5Yndc4swOun0L3D6TYk-8gdVXUJDRjbv1ZuhZltsw'.

Il componente è ancora pre-release, quindi questo potrebbe essere un difetto nella realizzazione, ma voglio pensare che sia il mio errore fino a quando tutte le possibilità sono state escluse.

C'è qualcosa che sto facendo che è ovviamente sbagliato, o c'è qualcosa che dovrei fare per capire esattamente perché la firma non riesce a essere convalidata?

risposta

5

Il problema si trova nel messaggio di eccezione qui:

Clausola [0] = X509ThumbprintKeyIdentifierClause (Hash = 0xF8A59280B3D13777CC7541B3218480984F421450)

Il token viene firmato con la clausola identificatore chiave predefinita per X Certificato .509: la sua impronta digitale. I metadati espongono solo i parametri RSA e un identificatore di nome. Quando il client recupera i metadati, imposta una chiave RSA utilizzando queste informazioni, non una identificazione personale X.509.

Per correggere questo errore, le credenziali di firma devono essere modificate per includere il nome corretto identificativo:

var credentials = new X509CertificateCredentials(
    cert, 
    new SecurityKeyIdentifier(
     new NamedKeySecurityKeyIdentifierClause(
      "kid", 
      "F8A59280B3D13777CC7541B3218480984F421450"))); 

Questo include l'identificatore previsto nella firma, e la firma viene convalidato correttamente.

+1

Per favore dimmi dove posso ottenere il certificato a cui ti riferisci? –

+1

Questo ha senso, ma da dove hai preso il certificato? –

+0

Stavo seguendo un tutorial e stavo usando il certificato temporaneo 'AddTemporarySigningCredential()', quindi dopo aver aggiornato l'app web, funziona. – Jaider

Problemi correlati