以下代码是我使用之前有关 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
在第三方应用程序中,它拒绝允许我这样做。
欢迎任何建议,我觉得我已经用尽了我的选择。
// 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];
错误就在这条线上。第二个参数需要设置为“nil”
NSURLCredential *credential = [NSURLCredential credentialWithIdentity:myIdentity certificates:(__bridge NSArray*)certsArray persistence:NSURLCredentialPersistenceNone];
变成:
NSURLCredential *credential = [NSURLCredential credentialWithIdentity:myIdentity certificates:nil persistence:NSURLCredentialPersistenceNone];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)