質問

みんな、おはよう、

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

どんな考えも感謝します!

認証のためのコード...

-(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;
        }
    }
}
役に立ちましたか?

解決

あなたはにのみ返信することができます 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];
}

両方の認証チャレンジを毎回取得しない場合、これはエラーではありません。資格情報を作成すると、資格情報をキャッシュできます。もしそうなら、あなたは必ずしも再び促されるわけではありません。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top