【发布时间】:2012-11-25 05:01:16
【问题描述】:
AFNetworking 是否支持 NTLM 身份验证?
我知道 ASIHTTPRequest 可以做到,我正在尝试迁移到 AFNetworking,但我必须确保它能够处理它。
我确实在互联网上搜索过这个,但我无法找到这个确切的答案。
谢谢大家。
【问题讨论】:
标签: ios afnetworking ntlm
AFNetworking 是否支持 NTLM 身份验证?
我知道 ASIHTTPRequest 可以做到,我正在尝试迁移到 AFNetworking,但我必须确保它能够处理它。
我确实在互联网上搜索过这个,但我无法找到这个确切的答案。
谢谢大家。
【问题讨论】:
标签: ios afnetworking ntlm
是的,AFNetworking 确实支持 NTLM 身份验证(或基本上任何身份验证方法),通常通过对身份验证挑战提供基于块的响应。
这是一个代码示例(假设 operation 是 AFHTTPRequestOperation、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];
}
}];
像往常一样开始或排队操作,这应该可以解决问题。
这基本上就是 Wayne Hartman describes in his blog 应用于 AFNetworking 的方法。
【讨论】: