以下代码是我使用以前帖子中有关 ios 客户端证书身份验证的代码。每个人都说这有效,但是为什么它对我有效?!?!
我不断收到以下错误:
connection didFailWithError: Error Domain=NSURLErrorDomain Code=-1206 "The server “www.mywebsite.com” requires a client certificate.
我在 stackoverflow 上关注了至少 4 篇关于同一主题的帖子,在其他类似网站上关注了 2 篇。他们都说同样的话,只是它不起作用。
我知道我的PKCS12文件有效,因为我将它通过电子邮件发送到我的 ipad,我将它安装在 ipad 钥匙串上,并且我能够使用 Safari 访问和授权证书身份。只是当我尝试NSURLConnection
在 3rd 方应用程序中使用它时,它拒绝让我这样做。
欢迎任何建议,我觉得我已经用尽了我的选择。
// Download PKCS12 Cert from my FTP server.
NSString *stringURL = @"ftp://user:password@myipaddress/myclientId.p12";
NSString* webStringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:webStringURL];
NSData *p12Data = [NSData dataWithContentsOfURL:url];
if ( p12Data )
{
CFDataRef inP12data = (__bridge CFDataRef)p12Data;
SecIdentityRef myIdentity;
SecTrustRef myTrust;
OSStatus errMsg = NULL;
// This is the same function that has been reposted in all the other posts about ios client certification authentication. So i'm not going to repost that.
errMsg = extractIdentityAndTrust(inP12data, &myIdentity, &myTrust);
SecCertificateRef myCertificate;
SecIdentityCopyCertificate(myIdentity, &myCertificate);
const void *certs[] = { myCertificate };
CFArrayRef certsArray = CFArrayCreate(NULL, certs, 1, NULL);
NSURLCredential *credential = [NSURLCredential credentialWithIdentity:myIdentity certificates:(__bridge NSArray*)certsArray persistence:NSURLCredentialPersistenceNone];
[[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];