AFNetworking setAuthenticationChallengeBlock [英] AFNetworking setAuthenticationChallengeBlock

查看:235
本文介绍了AFNetworking setAuthenticationChallengeBlock的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务器需要客户端证书,经过一段时间搜索和阅读AFNetworking文档中的示例后,我尝试设置setAuthenticationChallengeBlock并提供客户端证书。

My server requires a client certifiacte, after some time searching and reading examples in AFNetworking docs I tried to set setAuthenticationChallengeBlock and provide a client certificate.

在浏览器提供的certifacete中工作正常。

In browser provided certifacete works fine.

[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错误。

but the code inside block is never being called and server returns 403 error as expected.

setUploadBlock等其他块中的代码运行正常。

The code in other blocks such as setUploadBlock etc. works fine.

知道我的错误在哪里?

推荐答案

今晚我遇到了类似的问题。在进一步调查AFNetworking头文件后,我发现了我的问题。我忘了在我的操作中设置 setAuthenticationAgainstProtectionSpaceBlock 块。

I ran into a similar issue tonight. After further investigation of the AFNetworking header files I found my issue. I was forgetting to set the setAuthenticationAgainstProtectionSpaceBlock block on my operation.

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

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

        return YES;

    }];

我相信AFNetworking使用此块来处理NSURLConnectionDelegate协议方法: - ( BOOL)连接:(NSURLConnection *)连接canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace

I believe AFNetworking uses this block to handle the NSURLConnectionDelegate Protocol method: - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace.

这篇关于AFNetworking setAuthenticationChallengeBlock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆