2014-10-10 4 views
0

내 웹 응용 프로그램이 특정 서버와 통신하는 데 사용하는 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 
} 

답변

3

이 모든 것을 달성하기 위해 다양한 방법을 시도했지만 모두 정맥 상태였습니다.

그러나 간단한 트릭이 작동했습니다.

다시 웹 페이지를 강제로 적용하려면 아래 URL을 요청 URL 끝에 추가하십시오.

&r=1100910192 **(timestamp)**.

그래서 캐시 된 인증 된 신임장을 사용하는 대신 실제로 재 인증됩니다. 단지 요청의 끝 부분에 #을 추가하는 다른 경우

는 일 :

[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8080/some.json#"]] 

내가 도움이되기를 바랍니다.