HTTP 基本認証で小さな問題が 1 つあります。JSON を提供する Web サービス (REST) があります。サービスソースも、このサーバーへのアクセスもありません。
正しいユーザー名とパスワードを使用してサーバーで認証できます。私が使用しているコードは次のとおりです。
NSString *path = @"http://user:pwd@%bla.bla.com/api_key/";
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:path]];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:urlRequest
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"Response: %@", JSON);
if (successBlock) {
successBlock(...);
}
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
if (errorBlock) {
errorBlock(error, JSON);
}
}];
[operation setAuthenticationChallengeBlock:^(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge) {
if (challenge.previousFailureCount == 0) {
NSURLCredential *credentials = [NSURLCredential credentialWithUser:username password:password
persistence:(NSURLCredentialPersistenceNone)];
[[challenge sender] useCredential:credentials forAuthenticationChallenge:challenge];
}
else {
//[[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge];
[[challenge sender] cancelAuthenticationChallenge:challenge];
NSLog(@"Login error: Are credentials correct?");
if (errorBlock) {
//errorBlock(challenge.error, challenge.failureResponse);
}
}
NSURLRequest* req = connection.currentRequest;
AFJSONRequestOperation* op = operation;
NSLog(@"hdr %@",challenge.error);
}];
[operation setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) {
NSLog(@"redirect");
return request;
}];
[operation start];
問題は、認証の失敗時にサーバーから送信された追加のデータを取得する方法がわからないことです (ユーザー名またはパスワードが間違っていたことを示す追加の文字列を送信します)。Wireshark アプリを使用して、すべての http 認証プロセスを間違ったパスワードでログに記録しました。
これは、1 回の認証試行 (パスワードが間違っている) のスクリーンショットです。
サーバーが実際にパスワードが正しくないという平文の通知を送信していることがわかりますが、それをどこでどのようにキャッチするかわかりません。
何か案は?
事前にthx。