此服务器的证书无效

时间:2014-01-17 23:23:04

标签: ios iphone nsurlconnection nsurlconnectiondelegate

我知道如果我使用以下nsurlconnectiondelegate它将被修复

  

- 连接:willSendRequestForAuthenticationChallenge: -   连接:canAuthenticateAgainstProtectionSpace

但我正在尝试使用

  

sendAsynchronousRequest:队列:completionHandler:

所以你没有得到回调。我查看了它说的苹果文档

如果需要进行身份验证才能下载请求,则必须将所需的凭据指定为URL的一部分。如果身份验证失败或缺少凭据,则连接将尝试在没有凭据的情况下继续。

我无法弄清楚如何做到这一点。当我抬起头时,我得到的是这个私人电话

  

+(void)setAllowsAnyHTTPSCertificate:(BOOL)inAllow forHost:(NSString *)inHost;

知道怎么做吗?

以下是我得到的错误

  

此服务器的证书无效。你可能正在连接   假装是“example.com = 0x8b34da0”的服务器   {NSErrorFailingURLStringKey = https://example.com/test/,   NSLocalizedRecoverySuggestion =您想要连接到服务器吗?   无论如何?,NSErrorFailingURLKey = https://example.com/test/,   NSLocalizedDescription =此服务器的证书无效。您   可能连接到假装是“example.com”的服务器   这可能会使您的机密信息面临风险。,   NSUnderlyingError = 0xa26c1c0“此服务器的证书是   无效。您可能正在连接到假装的服务器   “example.com”可能会使您的机密信息面临风险。“,   NSURLErrorFailingURLPeerTrustErrorKey =

9 个答案:

答案 0 :(得分:9)

您正在使用的网络服务器要求服务器信任身份验证,您需要使用适当的操作正确回复。您需要实现connection:willSendRequestForAuthenticationChallenge:委托方法并使用SecTrustRef对其进行身份验证。

更多信息可在此处找到: - https://developer.apple.com/library/ios/technotes/tn2232/_index.html

这是我修复错误的代码:

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    NSURLProtectionSpace *protectionSpace = [challenge protectionSpace];

    id<NSURLAuthenticationChallengeSender> sender = [challenge sender];

    if ([[protectionSpace authenticationMethod] isEqualToString:NSURLAuthenticationMethodServerTrust])
    {
        SecTrustRef trust = [[challenge protectionSpace] serverTrust];

        NSURLCredential *credential = [[NSURLCredential alloc] initWithTrust:trust];

            [sender useCredential:credential forAuthenticationChallenge:challenge];
    }
    else
    {
        [sender performDefaultHandlingForAuthenticationChallenge:challenge];
    }
}

答案 1 :(得分:4)

如果您使用的是AFNetworking,则可以使用以下代码:

(正如临时客户端解决方案!)

AFHTTPSessionManager * apiManager = [AFHTTPSessionManager initWithBaseURL:[NSURL URLWithString:baseURL];
AFSecurityPolicy *policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
policy.allowInvalidCertificates = YES;
apiManager.securityPolicy = policy;

答案 2 :(得分:3)

你不能用你尝试的方式解决它

  • 要么退回CFNetworking以允许不​​良证书
  • 将NSConnection与委托和undoc'd方法一起使用
  • 使用您找到的私有API

一切都不好。 CFNetwork现在必须适合苹果,但其他两种方法甚至不是appstore-safe

更好让服务器得到修复。这是最简单,最干净的

答案 3 :(得分:3)

尝试一下。

使用自定义会话配置启动会话,如下所示:

let session = URLSession(configuration: URLSessionConfiguration.ephemeral,
                                 delegate: self,
                                 delegateQueue: nil)

实施以下委托回调方法:

public func urlSession(_: URLSession, task _: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
    guard let serverTrust = challenge.protectionSpace.serverTrust else {
        return completionHandler(URLSession.AuthChallengeDisposition.useCredential, nil)
    }
    return completionHandler(URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust: serverTrust))
}

答案 4 :(得分:1)

使用块尝试的方式无法解决此问题。您需要设置委托并实施身份验证质询代理以绕过证书验证。 最好的解决方案是创建一个正确的证书(确保它不是自签名的),或者如果你对它没有问题,可以将协议更改为HTTP。

答案 5 :(得分:0)

这是证书错误。您需要更改设置,以便您的程序/ os忽略证书,或将URL /证书添加到可信列表。

很抱歉,这是身份验证,证书是身份验证。我看了看,发现了这篇文章。

不确定它是否能解决您的问题,但它基本上表明,它们不包括使用文档中的证书连接到网站的情况。

http://www.cocoanetics.com/2009/11/ignoring-certificate-errors-on-nsurlrequest/

答案 6 :(得分:0)

答案 7 :(得分:0)

就我而言,由于我的防火墙阻止了所需的网址,因此发生了此错误。删除防火墙限制后,它工作正常

答案 8 :(得分:0)

就我而言,此错误是由于我的系统日期引起的。它被设置为旧日期,并且证书从该旧日期开始生效。更正日期后,它就可以使用。

相关问题