2012-11-25 139 views
5

AFNetworking是否支持NTLM身份验证?AFNetworking是否支持NTLM身份验证?

我知道ASIHTTPRequest可以做到这一点,我试图迁移到AFNetworking,但我必须确定它将能够处理它。

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

谢谢大家。

+0

可能重复[AFNetworking NTLM身份验证?](http://stackoverflow.com/questions/12483465/afnetworking-ntlm-authentication) –

回答

6

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

下面是一个代码示例(假设operationAFHTTPRequestOperation,AFJSONRequestOperation等)。在开始操作之前,设置如下认证质询模块:

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

像往常一样启动或排队操作,并且应该这样做。

这基本上是韦恩哈特曼describes in his blog方法适用于AFNetworking。

+0

谢谢先生,我不知道我可以通过一个块来处理验证! 我打算尽快尝试,但在我的项目中,我完成了使用块语法在ASIHTTPRequest上创建一个小的库,这就是我正在寻找的=) –

+0

开始使用您的方法,效果很好! 谢谢sir =) –

+0

嗯,setAuthenticationChallengeBlock似乎是连接操作的属性,而不是请求操作。 – fizgig