2011-12-19 5 views
0

기본 HTTP 인증을 수행하는 코드를 사용합니다 (아래 참조). 이것은 IOS 5에서 잘 작동합니다. 그러나 이제 프로토콜을 https로 변경했으며 가짜 자체 서명 인증서를 사용했습니다. 그것은 또한 일했다! 이것은 안전하지 않은 것처럼 보인다. 특정 인증서를받지 못하게하려면이 방법으로 무언가를해야하는지 알 수 있습니까?ios5에서 자체 서명 된 SSL 인증서 방지

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge: 
     (NSURLAuthenticationChallenge *)challenge { 

if ([challenge previousFailureCount] <= maxRetryCount) { 
    NSURLCredential *newCredential = 
    [NSURLCredential 
    credentialWithUser: userName 
    password:password 
    persistence:NSURLCredentialPersistenceForSession]; 

    [[challenge sender] 
    useCredential:newCredential 
    forAuthenticationChallenge:challenge]; 

    } 
    else 
    { 
    NSLog(@"Failure count %d",[challenge previousFailureCount]); 
    } 
} 

답변

1

나는 그 답을 스스로 찾았습니다. 이렇게하면 유효하지 않은 인증서가 차단됩니다. 유효한 인증서로 로그인 할 때 여전히 작동하는지 테스트해야합니다. 거기에 적절한`NSURLAuthenticationMethodServerTrust` 상수가 대신 사용한다고하는 것이

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge: 
     (NSURLAuthenticationChallenge *)challenge { 

    if ([[[challenge protectionSpace] authenticationMethod] isEqualToString:@"NSURLAuthenticationMethodServerTrust"]) { 
     [[challenge sender] performDefaultHandlingForAuthenticationChallenge:challenge]; 
    } 
    else { 
     if ([challenge previousFailureCount] <= maxRetryCount) { 
     NSURLCredential *newCredential = 
     [NSURLCredential 
     credentialWithUser: userName 
     password:password 
     persistence:NSURLCredentialPersistenceForSession]; 

     [[challenge sender] 
     useCredential:newCredential 
     forAuthenticationChallenge:challenge]; 

     } 
     else 
     { 
     NSLog(@"Failure count %d",[challenge previousFailureCount]); 
     } 
    } 
} 
+2

주 –

관련 문제