如何在 Cocoa 中使用 WebView 连接客户端证书?
我正在尝试连接到需要客户端证书的服务器。 因此,浏览到此服务器时发生的正常事件流程是 Web 浏览器(Safari 和 Chrome)提示用户选择证书并重试操作。
那么如何在 Cocoa 项目的嵌入式 WebView 中实现这一点呢? 到目前为止,我已经确定该错误是在 didFailProvisionalLoadWithError
方法中引发的:
- (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
NSLog(@"webView:didFailProvisionalLoadWithError:forFrame:");
NSLog(@" error = %@", error);
}
该错误确实是 error = Error Domain=NSURLErrorDomain Code=-1206 UserInfo=0x1006a8030 "The server “myserver.xxx”需要客户端证书。
但是如何显示一个对话框以便用户可以从钥匙串中选择证书?
I am trying to connect to a server that requires a client certificate.
So the normal flow of events that happens when browsing to this server is that the web browser (both Safari and Chrome) prompts the user to select a certificate and retry the operation.
So how can I accomplish this in a embedded WebView in a Cocoa project?
I have so far identified that the error is raised in the didFailProvisionalLoadWithError
method:
- (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
NSLog(@"webView:didFailProvisionalLoadWithError:forFrame:");
NSLog(@" error = %@", error);
}
The error is indeed error = Error Domain=NSURLErrorDomain Code=-1206 UserInfo=0x1006a8030 "The server “myserver.xxx” requires a client certificate.
But how can I display a dialog so that the user can select a certificate from the keychain?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题解决了。
WebView 组件的一个(已知)问题是罪魁祸首。
向 Apple 提交了 DTS 支持票并找到了解决方法。
编辑:
这是 DTS 的解决方法(我不知道这是否仍然有效,因为那是 3 年前的事了):
Problem solved.
A (known) issue with the WebView component was the culprit.
Opened up a DTS support ticket with Apple and got a workaround.
EDIT:
Here's the workaround from DTS ( I have no idea if this is still valid, since it was 3 years ago):
设置 WebResourceLoadDelegate 并实现身份验证挑战相关的委托方法。收到身份验证质询时,系统会提示您,此时您可以提供要使用的证书。
预计到达时间:以下是如何从存储在
clientSide.p12
中的证书创建NSURLCredential
:这来自另一个问题。您可能还会发现这个问题很有帮助。我通过谷歌搜索“nsurlcredential 证书”找到了这些。
Set a
WebResourceLoadDelegate
and implement the authentication-challenge–related delegate methods. You will be prompted when an authentication challenge is received, at which time you can provide the certificate to use.ETA: Here is how you can create an
NSURLCredential
from a certificate stored inclientSide.p12
:This comes from another question. You might also find this question helpful. I found these by Googling for "nsurlcredential certificate".