2013-04-21 16 views
6

Il mio server richiede un certificato cliente, dopo un po 'di tempo a cercare e leggere esempi in documenti AFNetworking ho provato a impostare setAuthenticationChallengeBlock e fornire un certificato client.AFNetworking setAuthenticationChallengeBlock

Nel browser fornito certifacete funziona bene.

[requestOperation setAuthenticationChallengeBlock:^(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge) 
    { 
     NSLog(@"AuthenticationChallenge"); 

     NSString *thePath = [[NSBundle mainBundle] pathForResource:@"client" ofType:@"pfx"]; 
     NSData *PKCS12Data = [[NSData alloc] initWithContentsOfFile:thePath]; 
     CFDataRef inPKCS12Data = (__bridge CFDataRef)PKCS12Data; 
     SecIdentityRef identity; 

     [self extractIdentity:inPKCS12Data :&identity]; 

     SecCertificateRef certificate = NULL; 
     SecIdentityCopyCertificate (identity, &certificate); 

     const void *certs[] = {certificate}; 
     CFArrayRef certArray = CFArrayCreate(kCFAllocatorDefault, certs, 1, NULL); 

     NSURLCredential *credential = [NSURLCredential credentialWithIdentity:identity certificates:(__bridge NSArray*)certArray persistence:NSURLCredentialPersistencePermanent]; 
     [challenge.sender useCredential:credential forAuthenticationChallenge:challenge]; 
    }]; 
    [requestOperation start]; 

ma il codice nel blocco non viene mai chiamato e il server restituisce errore 403 come previsto.

Il codice in altri blocchi come setUploadBlock ecc. Funziona correttamente.

Qualche idea di dove è il mio errore?

risposta

4

Mi sono imbattuto in un problema simile stasera. Dopo ulteriori indagini sui file header di AFNetworking ho trovato il mio problema. Mi stavo dimenticando di impostare il blocco setAuthenticationAgainstProtectionSpaceBlock sulla mia operazione.

[requestOperation setAuthenticationAgainstProtectionSpaceBlock:^BOOL(NSURLConnection *connection, NSURLProtectionSpace *protectionSpace) { 

     NSLog(@"Auth against protected space [%@]", protectionSpace); 

     return YES; 

    }]; 

credo AFNetworking utilizza questo blocco per gestire il metodo NSURLConnectionDelegate protocollo: - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace.

+0

Grazie, questo è excatctly quello che ho incasinato. –

+2

Si prega di mostrare un esempio completo? – Christian

+0

Mi piacerebbe anche un esempio completo. – lostintranslation