我们正尝试在企业iOS应用中使用客户端SSL证书进行用户身份验证.
我们如何让这个工作?谢谢!
大家早上好,
我一直在尝试编写一个从需要身份验证的远程Web服务执行某些GET的应用程序.我的主要问题是这些远程服务器中的大多数(并且有很多远程服务器)没有有效的证书.我有代码接受无效的证书和代码,以正确的uname&pass(下面)来响应挑战.我遇到的问题是让两人一起玩.我似乎无法找到一种方法来发送挑战NSURLCredential或正确链接回调的方法.当我试图将它们连接起来时,我无法NSURLRequest接到didReceiveAuthenticationChallenge两次电话.
任何想法将不胜感激!
认证代码......
-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
if(!hasCanceled){
if ([challenge previousFailureCount] == 0) {
NSURLCredential *newCredential;
newCredential=[NSURLCredential credentialWithUser:_username password:_password persistence:NSURLCredentialPersistenceNone];
[[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
}
else {
[[challenge sender] cancelAuthenticationChallenge:challenge];
NSLog(@"Bad Username Or Password");
badUsernameAndPassword = YES;
finished = YES;
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用 iOS 10。我正在评估自签名证书,如下所示
-(void) connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
NSURLProtectionSpace *protectionSpace = [challenge protectionSpace];
if ([protectionSpace authenticationMethod] == NSURLAuthenticationMethodServerTrust) {
SecTrustRef trust = [protectionSpace serverTrust];
SecPolicyRef policyOverride = SecPolicyCreateSSL(true, (CFStringRef)@"HOSTNAME");
SecTrustSetPolicies(trust, policyOverride);
CFMutableArrayRef certificates = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
/* Copy the certificates from the original trust object */
CFIndex count = SecTrustGetCertificateCount(trust);
CFIndex i=0;
for (i = 0; i < count; i++) {
SecCertificateRef item = SecTrustGetCertificateAtIndex(trust, i);
CFArrayAppendValue(certificates, item);
}
/* Create a new trust object */
SecTrustRef newtrust …Run Code Online (Sandbox Code Playgroud)