如何在 iOS App 中使用客户端证书认证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3749429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:45:41  来源:igfitidea点击:

How to use Client Certificate Authentication in iOS App

authenticationclientioscertificate

提问by asedra_le

I don't have a lot experience about Client Certificate Authentication. Anybody can tell me how to use it in iOS app? Thanks :)

我对客户端证书身份验证没有很多经验。谁能告诉我如何在iOS应用程序中使用它?谢谢 :)

回答by Jake

Your NSURLConnectiondelegate should respond to the connection:didReceiveAuthenticationChallenge:delegate method (see link below).

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:

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

It should respond by asking the challenge for its 'sender' and providing it with an appropriate credential.

它应该通过向其“发送者”询问挑战并为其提供适当的凭证来做出回应。

Something like:

就像是:

- (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];
}

See the NSURLCredentialclass reference for details of how to create a credential based on a certificate:

有关如何基于证书创建凭据的详细信息,请参阅NSURLCredential类参考:

回答by Balki

Before using client certificates in your app (as already answered by Jake) you have to implement import of certificate within your app to your app keychain. (note you need to use PKCS#12 certificate format, but you need to register it in your app (search for exported UTIs and Document types) with different extension, other than ".p12", which is already registered by the iOS. I've used .x-p12 in my app)

在您的应用程序中使用客户端证书之前(如 Jake 已经回答),您必须在您的应用程序中将证书导入到您的应用程序钥匙串。(注意您需要使用 PKCS#12 证书格式,但您需要在您的应用程序中使用不同的扩展名注册它(搜索导出的 UTI 和文档类型),而不是“.p12”,它已经被 iOS 注册。我已经在我的应用程序中使用了 .x-p12)

Or you need to include the certificate with your app bundle.

或者您需要将证书包含在您的应用程序包中。

See here: iOS Client Certificates and Mobile Device Management

请参阅此处:iOS 客户端证书和移动设备管理

and here: https://developer.apple.com/library/ios/qa/qa1745/_index.html

在这里:https: //developer.apple.com/library/ios/qa/qa1745/_index.html