AFNetworking是否支持NTLM身份验证?

时间:2012-11-25 05:01:17

标签: ios afnetworking ntlm

AFNetworking是否支持NTLM身份验证?

我知道ASIHTTPRequest可以做到,我正在尝试迁移到AFNetworking,但我必须确保它能够处理它。

我真的在互联网上搜索了这个,但我无法找到这个确切的答案。

谢谢大家。

1 个答案:

答案 0 :(得分:6)

是的,AFNetworking通常通过提供基于块的响应来验证身份验证挑战,从而支持NTLM身份验证(或基本上任何身份验证方法)。

以下是一个代码示例(假设operationAFHTTPRequestOperationAFJSONRequestOperation等。在开始操作之前,请设置身份验证质询块,如下所示:

[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];
   }
}];

像往常一样启动或排队操作,这应该可以解决问题。

这基本上是Wayne Hartman describes in his blog应用于AFNetworking的方法。

相关问题