【问题标题】:Does AFNetworking support NTLM authentication?AFNetworking 是否支持 NTLM 身份验证?
【发布时间】:2012-11-25 05:01:16
【问题描述】:

AFNetworking 是否支持 NTLM 身份验证?

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

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

谢谢大家。

【问题讨论】:

标签: ios afnetworking ntlm


【解决方案1】:

是的,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 的方法。

【讨论】:

  • 谢谢先生,我不知道我可以通过一个块来处理身份验证!我很快就会尝试,但在我的项目中,我完成了一个基于 ASIHTTPRequest 的小库,它使用块语法,这就是我想要的 =)
  • 开始使用你的方法,效果很好!谢谢先生=)
  • 嗯,setAuthenticationChallengeBlock 似乎是连接操作的属性,而不是请求操作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-04
  • 1970-01-01
  • 2013-12-24
  • 1970-01-01
  • 2020-07-12
  • 2016-05-01
  • 2020-01-08
相关资源
最近更新 更多