如何根据服务器指定多个证书?
session:didReceiveChallenge方法中,我可以返回NSURLCredential,但是我没有找到一种方法来识别挑战来自哪个URL。

我想做这样的事情:

-(void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
{

  // get the certificate depending on the url
  NSString *certificatePath;
  if ([url isEqualToString: @"server1.com"]) {
      certificatePath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/server1.p12"];
  } else if ([url isEqualToString: @"server2.com"]) {
      certificatePath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/server2.p12"];
  }

  //... some certificate stuff

  NSURLCredential *credential = [NSURLCredential credentialWithIdentity:myIdentity certificates:(__bridge NSArray*)certsArray persistence:NSURLCredentialPersistencePermanent];
  completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
}

最佳答案

我终于找到了答案,您可以像这样获得NSURLAuthenticationChallenge的主机:

NSString* host = challenge.protectionSpace.host;

而已

08-05 01:24