AFNetworking NTLM Authentication?

后端 未结 2 705
旧巷少年郎
旧巷少年郎 2020-12-11 09:59

I am banging my head trying to get AFNetworking to work since this is my first app that had to deal with Client/Server where I am trying to grab the JSON from a HTTPS server

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-11 10:44

    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.

提交回复
热议问题