如何在iOS应用程序中使用客户端证书认证

15

我对客户端证书认证并没有太多经验。有人可以告诉我如何在iOS应用程序中使用它吗?谢谢:)


可能是重复的问题: iPhone:HTTPS客户端证书认证 - user23743
2个回答

21

你的 NSURLConnection 代理应该响应 connection:didReceiveAuthenticationChallenge: 代理方法(见下面的链接)。

http://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/Classes/NSURLConnection_Class/Reference/Reference.html#//apple_ref/occ/instm/NSObject/connection:didReceiveAuthenticationChallenge:

它应该通过询问挑战的“sender”并为其提供适当的凭据来进行响应。

例如:

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
  id sender = [challenge sender];

  // create a credential from a certificate
  // see doco for details of the parameters
  NSURLCredential *creds = [NSURLCredential credentialWithIdentity:ident certificates:certs persistence:persistence];

  [sender useCredential:creds forAuthenticationChallenge:challenge];
}

查看NSURLCredential类参考资料,了解如何基于证书创建凭据的详细信息:


我说didReceiveAuthenticationChallenge现在已经被弃用了,这样说对吗?http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSURLConnectionDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intf/NSURLConnectionDelegate 有人能给我指一下如何使用客户端证书进行身份验证的更完整的示例吗? - Rory

3
使用客户端证书之前(如Jake所回答的),您需要在您的应用程序中实现证书的导入到您的应用程序钥匙串中(请注意,您需要使用PKCS#12证书格式,但是您需要使用不同于“.p12”的扩展名,在您的应用程序中注册它(搜索导出UTIs和文档类型),因为“.p12”已经被iOS注册。我在我的应用程序中使用了“.x-p12”)。
或者,您可以将证书包含在您的应用程序束中。
参见此处:iOS Client Certificates and Mobile Device Management 和此处:https://developer.apple.com/library/ios/qa/qa1745/_index.html

这适用于React Native应用程序吗? - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接