使用NSURLConnection的HTTPS - NSURLErrorServerCertificateUntrusted

Bri*_*ian 4 iphone ssl https cocoa-touch certificate

我有一个通过http连接的应用程序.尝试https时,我收到的错误表明根证书不受信任.我找到了我的站点证书,其CA证书和CA的根证书的URL,并通过Safari将它们添加到手机中.现在,当我转到首选项 - >常规 - >配置文件时,我可以看到所有证书一直在链上.每个证书上都有一个红色的未签名标签.当我连接时,我得到NSURLErrorServerCertificateUntrusted错误.我想弄清楚接下来要去哪里.

任何帮助都会很棒.唯一可能影响这一点的是我连接到一个奇数端口.所以我的网址是www.domain.com:port.端口号是否创建证书 - 域名不匹配?

现在我使用iPhone配置实用程序将配置配置文件添加到手机.它有我的根证书,ca证书和站点证书.手机上的个人资料显示已经过验证.在细节中,我可以看到我的三个证书.但是当我尝试连接时,仍然会收到不受信任的证书错误.有什么建议?

试着看看是否有其他人可以帮忙解决这个问题?

小智 11

在NSURLConnection加载期间,有一个支持的API用于忽略错误的证书.为此,只需将这样的内容添加到NSURLConnection委托:

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
  return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
  if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
    if (... user allows connection despite bad certificate ...)
      [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];

  [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
Run Code Online (Sandbox Code Playgroud)

请注意,connection:didReceiveAuthenticationChallenge:如果需要,在向用户显示对话框之后,可以稍后将其消息发送到challenge.sender(等等).