ios - 如何在 IOS 中检查所有 NSURLConnections 的 SSL 证书

标签 ios ssl nsurlconnection

现在我使用 -connection:didReceiveAuthenticationChallenge: 函数来检查 SSL 证书。根据用户的要求,我需要检查所有的请求。但在我的测试演示中,-connection:didReceiveAuthenticationChallenge: delegate 函数将在 5 分钟内仅调用一次。 5 分钟后,它将再次调用。但是我们的用户可能会在 5 分钟内发送多个请求。有谁知道必须解决这个问题吗?

请求码

NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:_server_url]];
[urlRequest setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[query dataUsingEncoding:NSUTF8StringEncoding]];

urlConnection = [[[NSURLConnection alloc] initWithRequest:urlRequest delegate:self] autorelease] ;
[urlConnection scheduleInRunLoop:[NSRunLoop mainRunLoop]
                      forMode:NSDefaultRunLoopMode];
[urlConnection start];

委托(delegate)函数:

- (BOOL)connection:(NSURLConnection *)conn canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
    NSLog(@"authenticate method:%@",protectionSpace.authenticationMethod);

    return YES;
}

- (void) connection:(NSURLConnection *)conn didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 
{
    NSURLAuthenticationChallenge *_challenge=[challenge retain];

    SecTrustRef trustRef = [[_challenge protectionSpace] serverTrust];
    SecTrustEvaluate(trustRef, NULL);

    SecCertificateRef certRef = SecTrustGetCertificateAtIndex(trustRef, 0);

    NSData *certificateData = (NSData *) SecCertificateCopyData(certRef);
    const unsigned char *certificateDataBytes = (const unsigned char *)[certificateData bytes];
    X509 *certificateX509 = d2i_X509(NULL, &certificateDataBytes, [certificateData length]);

    NSString *subject = CertificateGetDomainName(certificateX509);

    NSLog(@"Subject: %@", subject);

    [[_challenge sender] continueWithoutCredentialForAuthenticationChallenge:_challenge];

}

最佳答案

没有刷新 TLS 缓存的简单方法:http://developer.apple.com/library/ios/#qa/qa1727/_index.html

尝试重新考虑您的用例以确定您是否真的每次都需要身份验证。

关于ios - 如何在 IOS 中检查所有 NSURLConnections 的 SSL 证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14598968/

相关文章:

ios - 如何在 iPhone 6 plus 上放大布局

ios - 应用程序在后台ios时如何录制视频

iphone - 如何使用 iOS 可达性

iphone - 使用 NSURL 方法的登录凭据问题

javascript - 如何在 PhoneGap 中验证无互联网访问权限?

ssl - 接受安全 websocket 连接的服务器证书

java - Jenkins CI - SSL 握手失败

负载均衡 Nginx 服务器上的 SSL

iOS TLS session 缓存凭据

ios - 如何从 Swift 更新 Parse 中的数组