2013-04-21 12 views
6

私のサーバにはクライアント証明書が必要ですが、しばらくしてからAFNetworkingドキュメントのサンプルを検索して読んで、setAuthenticationChallengeBlockを設定してクライアント証明書を提供しようとしました。AFNetworking setAuthenticationChallengeBlock

ブラウザでcertifaceteは正常に動作します。

[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]; 

ブロック内のコードは決して呼び出されず、サーバーは403エラーを予想どおりに返します。

setUploadBlockなどの他のブロックのコードは正常に動作します。

私の間違いはどこですか?

答えて

4

私は今夜同様の問題に遭遇しました。 AFNetworkingヘッダーファイルをさらに調査したところ、問題が見つかりました。私はsetAuthenticationAgainstProtectionSpaceBlockブロックを私の操作でセットすることを忘れていました。 - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace

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

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

     return YES; 

    }]; 

私はAFNetworkingがNSURLConnectionDelegateプロトコルメソッドを処理するために、このブロックを使用しています信じています。

+0

ありがとう、これはexcatctly私が混乱したものです。 –

+2

完全な例を表示できますか? – Christian

+0

私は完全な例が大好きです。 – lostintranslation