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
In What Format Is This Date String
Insert a Floating Action Button on Uitableview in Swift
Save The Exif Metadata Using The New PHPhotolibrary
How to Use The Coreaudio API in Swift
-Costtonode:' Not Sent to Gkgraphnode2D Subclass in Gameplaykit
Resize All Subview in Scrollviewdidzoom
Uiviewrepresentable Automatic Size - Passing UIkit UIview Size to Swiftui
iOS 8 Uitableview First Row Has Wrong Height
How to Change Brightness, Contrast and Saturation Using Cicolorcontrols via Uislider with Swift
Is There an iOS API for Accessing Call Logs
Error When Instantiating a UIfont in an Text Attributes Dictionary
Mfmailcomposeviewcontroller Without Mail App Installed
Viewdiddisappear Not Called When Use Presentviewcontroller
Uialertview's Textfield Does Not Show Keyboard in iOS8