Afnetworking Ntlm Authentication

AFNetworking NTLM Authentication?

If you're trying to use NTLM authentication with AFNetworking you could try the following:

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.

Does AFNetworking support NTLM authentication?

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.

Digest access authentication for AFNetworking

Here what worked for me

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSURLCredential *credential = [[NSURLCredential alloc] initWithUser:@"Username" password:@"Password" persistence:NSURLCredentialPersistenceForSession];
[manager setCredential:credential];

Using AFNetworking with authentication fails with correct credentials

Using the syntax @rog suggested in his comment, I was able to adapt the code from the second link in my question (concerning downloading) to allow for basic authentication, and completely eliminate the need to subclass AFHTTPClient all together. Here's the (redacted) code I used:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"ftp://myUsername:myPassword@www.mysite.net/myfile.sqlite"]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
NSURL *path = [[[app applicationDocumentsDirectory] URLByAppendingPathComponent:@"myfile"] URLByAppendingPathExtension:@"sqlite"];
operation.outputStream = [NSOutputStream outputStreamWithURL:path append:NO];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"Successfully downloaded file to path: %@",path);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"Error in AFHTTPRequestOperation in clickedButtonAtIndex on FlightInfoController.m");
NSLog(@"%@",error.description);
}];

[operation start];

This doesn't actually get me any closer to knowing why using the [[AFNetworkingHelper sharedManager] setUsername:@"myUsername" andPassword@"myPassword"]; failed, but on the plus side I'm getting the behavior I want from my app. I guess I can live with just that.



Related Topics



Leave a reply



Submit