Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does AFNetworking support NTLM authentication?

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.

like image 838
Crystian Leão Avatar asked Nov 25 '12 05:11

Crystian Leão


1 Answers

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.

like image 164
Michael Thiel Avatar answered Nov 15 '22 07:11

Michael Thiel