【问题标题】:Handling self signed certificates with NSURLConnection使用 NSURLConnection 处理自签名证书
【发布时间】:2014-01-17 06:09:46
【问题描述】:

我想允许带有 NSURLConnection 的自签名证书,前提是主机在受信任的列表中。

我看到很多人在做这样的事情:

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
    if ([protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
        if (allowSelfSignedCertForThisHost) {
            NSLog(@"Allowing self signed!");
            return YES;
        }
    }
    return NO;
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
        if ([trustedHosts containsObject:challenge.protectionSpace.host]) {
            [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
        }
    }
    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}

但是,我想知道您为什么要调用 useCredential:forAuthenticationChallenge,但之后还要调用 continueWithoutCredentialForAuthenticationChallenge

【问题讨论】:

    标签: objective-c ssl nsurlconnection


    【解决方案1】:

    只需使用iPhone Configuration UtilityApple Configurator 在设备上添加该证书。

    【讨论】:

    • 这不可行 - 它是由我要连接的主机发送的。
    • @Will,下载并安装。你不需要它的私钥。有证书就够了。
    • 是否有任何代码更改来识别此自定义证书?这似乎更简单、更安全。
    • 安装自定义证书后,是否需要以编程方式识别此证书?
    • @Will,什么都没有。如果你在 Safari 中打开你的链接,它也会显示证书是可信的。
    猜你喜欢
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-05
    • 2017-02-25
    • 2016-11-14
    • 2021-07-28
    相关资源
    最近更新 更多