我试图避免使用
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler
我知道您可以在
中存储凭据NSURLCredentialStorage * credentialStorage = [NSURLCredentialStorage sharedCredentialStorage];
我使用以下代码将客户端证书存储在凭据存储中
// Create Credential Store
NSURLCredentialStorage * credentialStore = [NSURLCredentialStorage sharedCredentialStorage];
// Create Configuration
NSURLSessionConfiguration * sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
[sessionConfiguration setURLCredentialStorage:credentialStore];
// Create Session
NSURLSession * session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:self.delegateOperationQueue];
// Create Credential
SecCertificateRef myCertificate;
SecIdentityCopyCertificate(request.clientIdentity, &myCertificate);
const void * certs[] = { myCertificate };
CFArrayRef certsArray = CFArrayCreate(NULL, certs, 1, NULL);
NSURLCredential * credential = [NSURLCredential credentialWithIdentity:request.clientIdentity certificates:(__bridge NSArray*)certsArray persistence:NSURLCredentialPersistencePermanent];
CFRelease(certsArray);
// Create Protection Space
NSString * host = [urlRequest.URL host];
NSInteger port = [[urlRequest.URL port] integerValue];
NSString * protocol = [urlRequest.URL scheme];
NSURLProtectionSpace * protectionSpace = [[NSURLProtectionSpace alloc] initWithHost:host port:port protocol:protocol realm:nil authenticationMethod:NSURLAuthenticationMethodClientCertificate];
// Add Credential to Shared Credentials
[credentialStorage setDefaultCredential:credential forProtectionSpace:protectionSpace];
我在此处添加后,会话/任务不会从商店中读取它。我实现了
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler
方法并能够以这种方式接受挑战,我可以通过以下方式获得默认凭证
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler
{
if ([[challenge protectionSpace] authenticationMethod] == NSURLAuthenticationMethodClientCertificate)
{
NSString * host = [[challenge protectionSpace] host];
NSInteger port = [[challenge protectionSpace] port];
NSString * protocol = [[challenge protectionSpace] protocol];
NSLog(@"%@ %ld %@",host,(long)port,protocol);
// Get Credential
NSURLCredential * credential = [[NSURLCredentialStorage sharedCredentialStorage] defaultCredentialForProtectionSpace:[challenge protectionSpace]];
}
// Defualt Handling
completionHandler(NSURLSessionAuthChallengePerformDefaultHandling,nil);
}
这对我来说意味着正确存储了凭证,但当我告诉完成处理程序执行默认处理时,它失败了,这就是我得到的错误。
2014-11-03 11:09:02.368 PCSRegistrationServer Application[5555:1507600] CFNetwork SSLHandshake failed (-9824 -> -9829)
2014-11-03 11:09:02.459 PCSRegistrationServer Application[5555:1507600] NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9829)
我是否考虑过这个错误,或NSURLCredentialStorage不适用于NSURLAuthenticationMethodClientCertificate