在我的 iOS 应用程序中,我试图连接到具有不受信任证书的服务器。
我正在按照此 url 中指定的程序处理这种情况:
https://developer.apple.com/library/mac/documentation/cocoa/conceptual/urlloadingsystem/Articles/AuthenticationChallenges.html
这工作正常。
现在我有一个要求,我需要显示与证书相关的详细信息,例如:
现在我有几个问题:
一季度。我如何获得上述登记信息?是否有任何 cocoa API 提供相同的功能?
Q2。通常在 Web 浏览器中,它会显示与该证书相关的所有详细信息。我们是否需要在 iOS 应用程序中遵循相同的行为?
请建议。
最佳答案
Q1:https://stackoverflow.com/a/8903088/2957168应该完全回答你的问题。如果您需要额外的解释 - 请发表评论。
Q2:这是你的决定 - 如果你的服务器运行你自己的证书并且你喜欢保持这种方式(就像企业应用程序一样),只需验证证书 SHA1 或 MD5 是你期望的(这就是我们在我们的应用)。
这意味着您需要一个实现 NSURLConnectionDelegate
的类或 NSURLConnectionDataDelegate
(如果您还需要数据处理程序)
然后实现这些方法:
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
NSLOG(@"connection canAuthaenticateAgainstProtectionSpace");
if (![Certificates verifyProtectionSpace:protectionSpace]) { //this to verify your own certificate which is self signed.
NSLOG(@"Bad Certificate, canceling request");
[connection cancel];
self.ended = true;
return false;
}
return true;
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
NSLOG(@"connection didReceiveAuthenticationChallenge");
if ([Certificates verifyProtectionSpace:challenge.protectionSpace]) { //this is where you verify the certificates again - for non self-signed ones usually.
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
} else {
[challenge.sender performDefaultHandlingForAuthenticationChallenge:challenge];
}
}
关于cocoa-touch - 在 iOS 应用中显示不受信任的证书信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20026532/