AFNetworking NTLM验证? [英] AFNetworking NTLM Authentication?

查看:171
本文介绍了AFNetworking NTLM验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我敲我的头,试图让AFNetworking的工作,因为这是我的第一个应用程序,不得不处理客户机/服务器,我想从一个需要用户名/密码HTTPS服务器抓取JSON。我得到了它有点迷上了应用程序,但它一直抛出一个401错误,我看它是基本身份验证的问题。

我基本上采取了Twitter的例子​​来自AFNetworking,它适应了我的项目。在AFHTTPClient的子类中,我加入了initWithBaseURL另一条线,它仍然抛出的错误。我加入行是setAuthorizationHeaderWithUsername

   - (ID)initWithBaseURL:(NSURL *)网址{
自= [超级initWithBaseURL:URL]
如果(!个体经营){
    回零;
}[个体经营registerHTTPOperationClass:[AFJSONRequestOperation类];//接受HTTP头;看到http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
[个体经营setDefaultHeader:@接受值:@应用程序/ JSON];
[个体经营setAuthorizationHeaderWithUsername:@名为myusername密码:@我的密码];返回自我;
}


解决方案

如果你想使用NTLM身份验证与AFNetworking你可以尝试以下方法:

AFNetworking通过提供基于块的响应的认证一般不挑战支持NTLM身份验证(或基本上任何认证方法)。

下面是一个code为例(假设操作 AFHTT prequestOperation AFJSONRequestOperation 等)。在开始之前的操作设置身份验证的挑战块这样的:

  [运行setAuthenticationChallengeBlock:
 ^(NSURLConnection的*连接,NSURLAuthenticationChallenge *挑战)
{
   如果([挑战protectionSpace]使用authenticationMethod] == NSURLAuthenticationMethodNTLM)
   {
      如果([挑战previousFailureCount] 0)
      {
         //避免过多的验证尝试失败可能锁定用户
         [挑战发件人] cancelAuthenticationChallenge:挑战]
      }
      其他
      {
         [挑战发件人] useCredential:[NSURLCredential credentialWithUser:@用户名密码:@密码持久性:NSURLCredentialPersistenceForSession] forAuthenticationChallenge:挑战]
      }
   }
   其他
   {
      //如果需要,或取消这样的权威性验证比NTLM其他方式:
      [挑战发件人] cancelAuthenticationChallenge:挑战]
   }
}];

启动或排队的运作如常,而且应该做的伎俩。

这基本上是韦恩·哈特曼描述了在他的博客适用于AFNetworking。

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 that requires a username/password. I got it somewhat hooked up to the app, but it keeps throwing a 401 Error which I looked it up to be Basic Authentication issue.

I basically took the twitter example from AFNetworking and adapted it to my project. In the subclass of the AFHTTPClient, I am adding another line in the initWithBaseURL and it still throws the error. The line I am adding is the setAuthorizationHeaderWithUsername

- (id)initWithBaseURL:(NSURL *)url {
self = [super initWithBaseURL:url];
if (!self) {
    return nil;
}

[self registerHTTPOperationClass:[AFJSONRequestOperation class]];

// Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
[self setDefaultHeader:@"Accept" value:@"application/json"];
[self setAuthorizationHeaderWithUsername:@"myusername" password:@"my password"];

return self;
}

解决方案

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.

这篇关于AFNetworking NTLM验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆