1

アプリ通信のために NSURLConnection から NSURLSession に切り替えていますが、その間に、委任された認証から NSURLCredentialStorage の利用に移行しようとしています。コードを移動しましたが、アプリの起動時に sharedCredentialStorage に defaultCredentials を設定したにもかかわらず、デリゲートで -URLSession:task:didReceiveChallenge が呼び出されています。

以下のログに記録されたメッセージによると、保護スペースは同一です (資格情報のセットアップ時に作成したものと NSURLAuthenticationChallenge によって渡されたもの)。

Register credentials for: <NSURLProtectionSpace: 0x162227c0>: Host:192.168.1.99, Server:https, Auth-Scheme:NSURLAuthenticationMethodDefault, Realm:192.168.1.99, Port:23650, Proxy:NO, Proxy-Type:(null)
Unexpected authentication challenge: <NSURLProtectionSpace: 0x1680ee40>: Host:192.168.1.99, Server:https, Auth-Scheme:NSURLAuthenticationMethodDefault, Realm:192.168.1.99, Port:23650, Proxy:NO, Proxy-Type:(null)

および didReceiveChallenge:(NSURLAuthenticationChallenge*)challenge デリゲート メソッド中:

po [[NSURLCredentialStorage sharedCredentialStorage] defaultCredentialForProtectionSpace:[challenge protectionSpace]]

結果は

<NSURLCredential: 0x1680ff00>: thecorrectusername

https://stackoverflow.com/a/501869/563905は、サーバーが 401 チャレンジで応答すると、NSURLConnection (これは NSURLSession の問題ですか?) が最初に承認のヘッダーをチェックし (セットはありません)、次に、保護スペースの資格情報について NSURLCredentialStorage を調べます。

didReceiveChallenge デリゲートが呼び出される理由がわかりません。デリゲートメソッドを設定していない場合、NSURLSession は資格情報なしでリクエストを再送信するだけです...困惑しています...

編集:手動の資格情報の処理を didReceiveChallenge: メソッドに追加しました。単一の NSURLSession しか使用されていないにもかかわらず、リクエストごとにトリガーされています。

4

2 に答える 2

2

私のURLSessionが保存された資格情報を使用しないという同じ問題がありました。次に、NSURLSession のリファレンス ドキュメントを読みました。基本的には、カスタム デリゲートを実装している場合、デリゲート メソッドが呼び出されたときにすべてのものを自分で処理する必要があるということです。言い換えれば、資格情報を保存することは戦いの半分です。サーバーが認証を要求するたびにチャレンジを受け取ることになるため、didReceiveChallenge メソッドでは、使用する資格情報を手動で抽出して完了ハンドラーに渡す必要があります。それが理にかなっているかどうか教えてください。

于 2014-11-07T17:10:53.120 に答える
0

NSURLSessionTaskDelegate または NSURLSessionDelegate を使用する必要があります。

//これは基本的な資格情報用です https://developer.apple.com/documentation/foundation/nsurlsessiontaskdelegate/1411595-urlsession

また

//これはセッション レベルのチャレンジ用です — NSURLAuthenticationMethodNTLM、NSURLAuthenticationMethodNegotiate、NSURLAuthenticationMethodClientCertificate、または NSURLAuthenticationMethodServerTrust https://developer.apple.com/documentation/foundation/nsurlsessiondelegate/1409308-urlsession

例えば:

@interface MySessionClass : URLSession <URLSessionTaskDelegate>

@end

@implementation MySessionClass

#pragma mark - Delegate

//That method will call one time if you use NSURLCredentialPersistencePermanent but if you use other type that method it will call all the time.
- (void) URLSession:(NSURLSession *)session 
               task:(NSURLSessionTask *)task 
 didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge 
  completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler {

if (challenge.previousFailureCount == 0) {
        NSURLCredential *credential = [NSURLCredential credentialWithUser:self.user password:self.password persistence:NSURLCredentialPersistencePermanent];
        completionHandler(NSURLSessionAuthChallengeUseCredential, credentials);
    } else {
        completionHandler(NSURLSessionAuthChallengeRejectProtectionSpace, nil);
    }
}

@end
于 2018-05-10T07:21:56.580 に答える