gpt4 book ai didi

ios - willSendRequestForAuthenticationChallenge 方法被递归调用

转载 作者:可可西里 更新时间:2023-11-01 06:02:28 27 4
gpt4 key购买 nike

我正在使用 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 = NULL;
if (SecTrustCreateWithCertificates(certificates, policyOverride, &newtrust) != errSecSuccess) {
/* Probably a good spot to log something. */
NSLog(@"Error in SecTrustCreateWithCertificates");
[connection cancel];
return;
}

CFRelease(policyOverride);

/* Re-evaluate the trust policy. */
SecTrustResultType secresult = kSecTrustResultInvalid;
if (SecTrustEvaluate(trust, &secresult) != errSecSuccess) {

/* Trust evaluation failed. */
[connection cancel];

// Perform other cleanup here, as needed.
return;
}

switch (secresult) {
//case kSecTrustResultInvalid:
//case kSecTrustResultRecoverableTrustFailure:
case kSecTrustResultUnspecified: // The OS trusts this certificate implicitly.
case kSecTrustResultProceed: // The user explicitly told the OS to trust it.
{
NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
[challenge.sender useCredential:credential forAuthenticationChallenge:challenge];

return;
}
default: ;
/* It's somebody else's key. Fall through. */
[challenge.sender performDefaultHandlingForAuthenticationChallenge:challenge];
break;
}
/* The server sent a key other than the trusted key. */
[connection cancel];
// Perform other cleanup here, as needed.
}
}

评估后的结果是“kSecTrustResultUnspecified”,同样的方法“willSendRequestForAuthenticationChallenge”被递归调用。不确定为什么递归调用该方法。让我知道代码的任何问题。

谢谢

最佳答案

有几个解决方案,我认为找到了最简单的一个 here .总之,您需要检查 [challenge previousFailureCount] 以防止一遍又一遍地重新进入该方法。

否则,根据 Apple API 文档,我会建议类似于 this 的内容,它使用已弃用的委托(delegate)回调,但可能对您有用。

关于ios - willSendRequestForAuthenticationChallenge 方法被递归调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39903619/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com