5

Does AFNetworking support NTLM authentication?

I know ASIHTTPRequest can do it, i'm trying to migrate to AFNetworking, but i have to be sure it will be able to deal with it.

I really searched the internet for this, but i was unable to find this exact answer.

Thank you all.

Crystian Leão
  • 695
  • 1
  • 8
  • 18
  • possible duplicate of [AFNetworking NTLM Authentication?](http://stackoverflow.com/questions/12483465/afnetworking-ntlm-authentication) – Şafak Gezer Sep 23 '14 at 09:57

1 Answers1

6

Yes, AFNetworking does support NTLM authentication (or basically any authentication method) by providing a block-based response to authentication challenges in general.

Here's a code example (assuming operation is a AFHTTPRequestOperation, AFJSONRequestOperation etc.). Before starting the operation set the authentication challenge block like this:

[operation setAuthenticationChallengeBlock:
 ^( NSURLConnection* connection, NSURLAuthenticationChallenge* challenge )
{
   if( [[challenge protectionSpace] authenticationMethod] == NSURLAuthenticationMethodNTLM )
   {
      if( [challenge previousFailureCount] > 0 )
      {
         // Avoid too many failed authentication attempts which could lock out the user
         [[challenge sender] cancelAuthenticationChallenge:challenge];
      }
      else
      {
         [[challenge sender] useCredential:[NSURLCredential credentialWithUser:@"username" password:@"password" persistence:NSURLCredentialPersistenceForSession] forAuthenticationChallenge:challenge];
      }
   }
   else
   {
      // Authenticate in other ways than NTLM if desired or cancel the auth like this:
      [[challenge sender] cancelAuthenticationChallenge:challenge];
   }
}];

Start or enqueue the operation as usual and that should do the trick.

This is basically the method Wayne Hartman describes in his blog applied to AFNetworking.

Michael Thiel
  • 2,434
  • 23
  • 21
  • Thank you sir, i didn't know that i could pass a block to deal with authentication! I'm going to try it soon, but in my project i finished up making an small lib over ASIHTTPRequest that uses block syntax, that is what i was looking for =) – Crystian Leão Jan 13 '13 at 22:59
  • Started using your approach, works very well! Thank you sir =) – Crystian Leão Jan 20 '13 at 23:12
  • Hmm, setAuthenticationChallengeBlock seems to be a property of a connection operation, not a request operation. – fizgig Jul 03 '14 at 15:35