2009-10-16 106 views
4

我有一个应用程序,通过http连接罚款。在尝试https时,我收到错误消息,说明根证书不受信任。我找到了我的站点证书,CA证书和CA的根证书的URL,并通过Safari将它们添加到了手机中。现在,当我进入首选项 - >常规 - >配置文件时,我可以看到我的所有证书都链接上了。每个证书上都有一个红色的未签名标签。仍然当我连接我得到NSURLErrorServerCertificateUntrusted错误。我试图找出接下来要去的地方。HTTPS与NSURLConnection - NSURLErrorServerCertificateUntrusted

任何帮助将是伟大的。唯一可能影响到这件事的是,我连接到一个奇怪的港口。所以我的网址是www.domain.com:port。端口号是否创建证书 - 域名不匹配?

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

只是想看看是否有其他人可以帮助吗?

回答

10

在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]; 
} 

注意连接:didReceiveAuthenticationChallenge:以后可以发送其消息challenge.sender(多),如果有必要提出一个对话框,给用户之后等

+1

你怎么知道什么对话框提出?似乎你可以在这里得到NSURLErrorServerCertificateUntrusted之外的原因(比如NSURLErrorServerCertificateHasBadDate,我认为这意味着它已经过期)。 –

+0

我们可以使用超级卡西尼的上述方法吗?现在我正在使用IIS并将以上方法用于不可信证书,但对于某些特殊情况,我还必须考虑超卡西尼。 – Pooja