내 웹 응용 프로그램이 특정 서버와 통신하는 데 사용하는 NSURLCredential을 전환 할 수있는 방법을 찾고 있습니다.iOS 특정 서버의 NSURLCredential을 재설정하거나 내 앱이 해당 서버와 통화 한 것을 잊어 버리는 방법은 무엇입니까?
주어진 서버 트러스트에 대한 NSURLCredential SSL 자격 증명을 "재설정"하고 다른 자격 증명을 사용하여 전체 핸드 셰이 킹 프로세스를 반복하도록 할 수있는 방법이 있습니까? 다른 말로하면, 내 웹 앱이이 서버와 통화 한 것을 잊어 버리고 싶습니다.
아래의 코드 스 니펫은 이전에 평가 한 자격 증명을 검색하는 것입니다.
- (void)customHTTPProtocol:(CustomHTTPProtocol *)protocol didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
NSURLCredential * credential = nil;
SecTrustRef trust;
NSURLProtectionSpace* protectionSpace = challenge.protectionSpace;
if ([protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
//this verifies that we are talking to a server that we trust by checking it's certificate
// Extract the SecTrust object from the challenge, apply our trusted anchors to that
// object, and then evaluate the trust. If it's OK, create a credential and use
// that to resolve the authentication challenge. If anything goes wrong, resolve
// the challenge with nil, which continues without a credential, which causes the
// connection to fail.
trust = [protectionSpace serverTrust];
if (trust == NULL) {
assert(NO);
} else {
// Just Ignore Self Signed Certs
SecTrustResultType result;
//This takes the serverTrust object and checkes it against your keychain
SecTrustEvaluate(protectionSpace.serverTrust, &result);
//if we want to ignore invalid server for certificates, we just accept the server
credential = [NSURLCredential credentialForTrust: challenge.protectionSpace.serverTrust];
if(result == kSecTrustResultProceed ||
// result == kSecTrustResultConfirm || // Deprecated - Remove?
result == kSecTrustResultUnspecified) {
[challenge.sender useCredential:credential forAuthenticationChallenge: challenge];
}
}
[protocol resolveAuthenticationChallenge:challenge withCredential:credential];
}
//... more ways to resolve challenge
}