此服务器的证书无效

问题描述:

我知道,如果我用以下nsurlconnectiondelegate这将是固定此服务器的证书无效

– connection:willSendRequestForAuthenticationChallenge: – connection:canAuthenticateAgainstProtectionSpace

但我试图用

sendAsynchronousRequest:queue:completionHandler:

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

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

我想不出如何做到这一点。当我抬头看我的一切是这样的私人电话

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

任何想法如何做到这一点?

以下是错误我得到

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=

你不能用你想

  • 要么降CFNetworking允许坏证书
  • 使用NSConnection连接用委托的方式和方法undoc'd修复它
  • 使用您发现的私有API

一切都不好。 CFNetwork现在必须为苹果行,但其他2种方法甚至没有appstore安全

更好获取服务器固定。这就是最简单,如果你正在使用AFNetworking干净

+0

原来呼叫被重新路由到服务器导致了这个问题。通过直接指向服务器来修复它。 – pa12

+0

@ Daij-Djan如果使用私有API,则应用程序在提交到App-Store时将被拒绝,以防App生产。 –

+0

是的,这是写我写它“现在的CFNetwork将不得不苹果,但其他2种方法甚至不是appstore安全” –

这是一个证书错误。您需要更改设置,以便程序/操作系统忽略证书,或将URL /证书添加到可信列表中。

不好意思是认证,证书是认证。我看了一下,发现了这篇文章。

不确定它是否可以解决您的问题,但它基本上声明,它们不涵盖连接到站点以及文档中证书的方式。

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

+0

你说得对。我的错。你能解释如何做到这一点? – pa12

+0

-1不好的建议你不能这样做:quote:“我收到一份报告,有人因为使用这种方法而拒绝了他的应用程序,所以你真的只能用它来测试。” –

,您可以使用此代码:(!就像一个临时的客户端解决方案)

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

如果AFNetworking不使用? –

+1

@JayprakashDubey那么看看接受的答案 – skywinder

您是网络服务器使用的是要求服务器信任身份验证,您需要适当的响应与适当的行动。您需要实施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]; 
    } 
} 
+0

太棒了,非常感谢。 –

+0

最佳解决方案,谢谢:) –

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

在我而言,这个错误发生,由于我的防火墙阻止所需的网址。在取消防火墙限制后它工作正常