6

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

ブラウザで提供された証明書は正常に動作します。

[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

1 に答える 1

4

今夜も同様の問題に遭遇しました。AFNetworking ヘッダー ファイルをさらに調査した結果、問題が見つかりました。setAuthenticationAgainstProtectionSpaceBlock操作にブロックを設定するのを忘れていました。

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

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

        return YES;

    }];

AFNetworking はこのブロックを使用して NSURLConnectionDelegate Protocol メソッドを処理していると思います: - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace.

于 2013-05-02T03:03:29.037 に答える