首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AFNetworking setAuthenticationChallengeBlock

AFNetworking setAuthenticationChallengeBlock
EN

Stack Overflow用户
提问于 2013-04-22 07:13:46
回答 1查看 4.4K关注 0票数 6

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

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

代码语言:javascript
复制
[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等)中的代码运行良好。

你知道我的错误在哪里吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-02 11:03:29

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

代码语言:javascript
复制
    [requestOperation  setAuthenticationAgainstProtectionSpaceBlock:^BOOL(NSURLConnection *connection, NSURLProtectionSpace *protectionSpace) {

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

        return YES;

    }];

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16137220

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档