ios 此服务器的证书无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21197834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:10:08  来源:igfitidea点击:

The certificate for this server is invalid

iosiphonensurlconnectionnsurlconnectiondelegate

提问by pa12

I know that if I use following nsurlconnectiondelegate it will be fixed

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

– connection:willSendRequestForAuthenticationChallenge: – connection:canAuthenticateAgainstProtectionSpace

– 连接:willSendRequestForAuthenticationChallenge: – 连接:canAuthenticateAgainstProtectionSpace

But I am trying to use

但我正在尝试使用

sendAsynchronousRequest:queue:completionHandler:

sendAsynchronousRequest:queue:completionHandler:

So you don't get the callback. I looked into apple docs it say following

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

If authentication is required in order to download the request, the required credentials must be specified as part of the URL. If authentication fails, or credentials are missing, the connection will attempt to continue without credentials.

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

I could not figure out how to do that. When I looked up all I got is this private call

我不知道该怎么做。当我抬头时,我得到的只是这个私人电话

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

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

Any idea how to do this?

知道如何做到这一点吗?

Following is the error I get

以下是我得到的错误

The certificate for this server is invalid. You might be connecting to a server that is pretending to be “example.com=0x8b34da0 {NSErrorFailingURLStringKey=https://example.com/test/, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, NSErrorFailingURLKey=https://example.com/test/, NSLocalizedDescription=The certificate for this server is invalid. You might be connecting to a server that is pretending to be “example.com” which could put your confidential information at risk., NSUnderlyingError=0xa26c1c0 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “example.com” which could put your confidential information at risk.", NSURLErrorFailingURLPeerTrustErrorKey=

此服务器的证书无效。您可能正在连接到一个假装是“example.com=0x8b34da0 {NSErrorFailingURLStringKey= https://example.com/test/, NSLocalizedRecoverySuggestion=Would you like to connect to the server always?, NSErrorFailingURLKey= https:/ /example.com/test/, NSLocalizedDescription=此服务器的证书无效。您可能正在连接到一个伪装成“example.com”的服务器,这可能会使您的机密信息处于危险之中。, NSUnderlyingError=0xa26c1c0 “该服务器的证书无效。您可能正在连接到一个伪装成是“example.com”,这可能会使您的机密信息处于危险之中。”, NSURLErrorFailingURLPeerTrustErrorKey=

采纳答案by Daij-Djan

you can't fix it with the way you are trying

你不能用你正在尝试的方式修复它

  • either drop to CFNetworking to allow bad certs
  • use NSConnection with a delegate and an undoc'd method
  • use the private API you found
  • 要么下降到 CFNetworking 以允许不良证书
  • 将 NSConnection 与委托和未记录的方法一起使用
  • 使用您找到的私有 API


all not good.CFNetwork would have to be OK for apple for now but the other 2 methods aren't even appstore-safe

都不好。CFNetwork 目前对苹果来说还可以,但其他 2 种方法甚至不是应用商店安全的

Betterget the server fixed. Thats the easiest and CLEANEST

最好把服务器修好。那是最简单和最干净的

回答by Mohd Iftekhar Qurashi

The webserver which you are using is asking for Server Trust Authentication, you need to properly respond with the appropriate action. You need to implement connection:willSendRequestForAuthenticationChallenge:delegate method and use SecTrustRef to authenticate it.

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

More information can be found here:- https://developer.apple.com/library/ios/technotes/tn2232/_index.html

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

This was my code to fix error:

这是我修复错误的代码:

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

回答by skywinder

If you are using AFNetworking, you can use this code:

如果您正在使用AFNetworking,则可以使用以下代码:

(Just as a temp client-side solution!)

(就像一个临时的客户端解决方案!)

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

回答by prajothdsa

This issue cannot be fixed with the way you are trying with blocks. you need to set delegates and implement the authentication challenge delegates to bypass the certificate validation. Best solution is to either create a right certificate (make sure it is not self-signed) or change the protocol to HTTP if you are fine with it.

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

回答by arunjos007

In my case, this error occurred due to my firewall blocked the required url. it's worked fine after removing firewall restrictions

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

回答by Bhanu Birani

Try this.

尝试这个。

Initiate your session using custom session config as shown below:

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

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

Implement the following delegate callback method:

实现以下委托回调方法:

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))
}

回答by Peter

In my case, this error occurred due to my system date. It was set as an old date, and the certificate is not effective from that old date. After correct the date, it works.

就我而言,此错误是由于我的系统日期造成的。它被设置为旧日期,并且证书从该旧日期开始无效。更正日期后,它可以工作。

回答by am6sigma

That is a certificate error. you need to change your settings so that your program/os ignores the certificate, or add the url/certificate to a trusted list.

那是证书错误。您需要更改设置,以便您的程序/操作系统忽略证书,或将 url/证书添加到受信任列表中。

Sorry that is authentication, certificate is authentication. I took a look, and I found this article.

对不起,这是身份验证,证书是身份验证。我看了一下,发现了这篇文章。

Not sure if it will resolve your issue, but it basically states, that they don't cover the case of connecting to a site with how a certificate in the documentation.

不确定它是否会解决您的问题,但它基本上表明,它们不涵盖使用文档中的证书如何连接到站点的情况。

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

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