2011-12-19 8 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

私は自分自身で答えを見つけたようです。これにより、無効な証明書がブロックされます。 有効な証明書でログインしても動作するかどうかテストする必要があります。

- (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

代わりに使用する必要がある適切なNSURLAuthenticationMethodServerTrust定数があることに注意してください –

関連する問題