8

みんな、おはよう、

認証が必要なリモート Web サービスからいくつかの GET を実行するアプリケーションを作成しようとしています。私の主な問題は、これらのリモート サーバーの大部分 (および多数のサーバー) が有効な証明書を持っていないことです。無効な証明書を受け入れるコードと、正しい uname & pass (以下) でチャレンジに応答するコードがあります。私が抱えている問題は、2つを一緒にプレイすることです。NSURLCredential両方のチャレンジを送信する方法、またはコールバックを正しくチェーンする方法を見つけることができないようです。それらをチェーンしようとすると、2回NSURLRequest呼び出すことができませんdidReceiveAuthenticationChallenge

どんな考えでも大歓迎です!

認証用コード...

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{   
    if(!hasCanceled){
        if ([challenge previousFailureCount] == 0) {
            NSURLCredential *newCredential;
            newCredential=[NSURLCredential credentialWithUser:_username password:_password persistence:NSURLCredentialPersistenceNone];
            [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
        } 
        else {
            [[challenge sender] cancelAuthenticationChallenge:challenge];
            NSLog(@"Bad Username Or Password");
            badUsernameAndPassword = YES;
            finished = YES;
        }
    }
}
4

1 に答える 1

9

NSURLAuthenticationChallengeそのチャレンジの資格情報のみを返信できます。以下を使用して、受け取ったチャレンジのタイプを判断できます。

[[challenge protectionSpace] authenticationMethod]

可能な値は、ここに記載されています。無効なサーバー証明書の場合、認証方法は になりますNSURLAuthenticationMethodServerTrust。コードでは、認証方法を確認し、適切に応答する必要があります。

if ([challenge previousFailureCount] > 0) {
    // handle bad credentials here
    [[challenge sender] cancelAuthenticationChallenge:challenge];
    return;
}

if ([[challenge protectionSpace] authenticationMethod] == NSURLAuthenticationMethodServerTrust) {
    // check if the user has previously accepted the certificate, otherwise prompt
} else if ([[challenge protectionSpace] authenticationMethod] == /* your supported authentication method here */) {
    [[challenge sender] useCredential:/* your user's credential */ forAuthenticationChallenge:challenge];
}

毎回両方の認証チャレンジを取得しなくてもエラーにはなりません。資格情報を作成するときに資格情報をキャッシュできます。その場合、必ずしも再度プロンプトが表示されるとは限りません。

于 2010-06-01T13:55:47.417 に答える