我的服务器需要客户端证书,在经过一段时间的搜索和阅读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等)中的代码运行良好。
你知道我的错误在哪里吗?
发布于 2013-05-02 11:03:29
我今晚也遇到了类似的问题。在进一步研究AFNetworking头文件之后,我发现了我的问题。我忘记在我的操作中设置setAuthenticationAgainstProtectionSpaceBlock块了。
[requestOperation setAuthenticationAgainstProtectionSpaceBlock:^BOOL(NSURLConnection *connection, NSURLProtectionSpace *protectionSpace) {
NSLog(@"Auth against protected space [%@]", protectionSpace);
return YES;
}];我相信AFNetworking使用这个块来处理NSURLConnectionDelegate协议方法:- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace。
https://stackoverflow.com/questions/16137220
复制相似问题