-1

I'm using AFNetworking. I've subclassed AFURLConnectionOperation and am using setWillSendRequestForAuthenticationChallengeBlock to override the handling of AuthenticationChallenge. I'm setting the Authorization header in every request and the server returns 401 for wrong credentials.

My use case is, if I send more than a certain number of requests with wrong credentials, the account will get locked out. I'm trying to prevent that by cancelling the authentication challenge so that only one request is sent.

[challenge.sender cancelAuthenticationChallenge:challenge];

But when I do this, the connection operation is failing with error -1012 (NSURLErrorDomain kCFURLErrorUserCancelledAuthentication) and thus I lose the actual error code 401.

Is there a way to cancel the challenge without failing the connection?

vivekDas
  • 1,248
  • 8
  • 12

1 Answers1

0

Canceling the authentication, by its nature, causes the request to fail. If you want to pass the failure along to get the error page body, rather than dropping the connection immediately, you can continue without credentials, and it will pass the error body to your app. Basically:

[[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge];

That said, I'm not sure why your app couldn't just treat kCFURLErrorUserCancelledAuthentication like it would treat a 401. :-)

dgatwood
  • 10,129
  • 1
  • 28
  • 49