【问题标题】:How to use AFNetworking 2 with Digest Authentication如何将 AFNetworking 2 与摘要式身份验证一起使用
【发布时间】:2014-03-29 20:43:03
【问题描述】:

我一直在搜索“带有摘要式身份验证的 AFNetworking 2”,但没有找到有关它的有用讨论(this one 除外,但不幸的是,它看起来像是针对 AFNetworking 1)。

这是我的未经身份验证的代码:

NSString* apiURL = @"https://api.example.com/WS.asmx";
AFHTTPSessionManager* manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager
    GET:apiURL 
    parameters: [NSDictionary dictionaryWithObjectsAndKeys:@"ID", 1234 , nil]
    success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
    }
    failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"WS request failed: %@", error);
    }
];

Digest Auth 代码在哪里以及如何发挥作用?

【问题讨论】:

    标签: objective-c ios7 afnetworking afnetworking-2 digest-authentication


    【解决方案1】:

    这个问题有点老了,但我不得不这样做,但我找不到任何内置的方法来处理它。 我使用的解决方案是编辑 AFNetworkings AFURLSessionManager.m。 基本上你修改这个委托方法来支持http摘要,这里有一个完整的例子。

        - (void)URLSession:(NSURLSession *)session
                      task:(NSURLSessionTask *)task
        didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
         completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
        {
            NSURLSessionAuthChallengeDisposition disposition = NSURLSessionAuthChallengePerformDefaultHandling;
            __block NSURLCredential *credential = nil;
    
            if (self.taskDidReceiveAuthenticationChallenge) {
                disposition = self.taskDidReceiveAuthenticationChallenge(session, task, challenge, &credential);
            } else {
                if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
                    if ([self.securityPolicy evaluateServerTrust:challenge.protectionSpace.serverTrust forDomain:challenge.protectionSpace.host]) {
                        disposition = NSURLSessionAuthChallengeUseCredential;
                        credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
                    } else {
                        disposition = NSURLSessionAuthChallengeCancelAuthenticationChallenge;
                    }
                } else if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPDigest]) {
                    NSDictionary *userkeys = [[NSURLCredentialStorage sharedCredentialStorage] credentialsForProtectionSpace:challenge.protectionSpace];
    
                    credential = [userkeys objectForKey:(NSString *)[[userkeys allKeys] objectAtIndex: 0]];
                    if (credential) {
                        disposition = NSURLSessionAuthChallengeUseCredential;
                    } else {
                        disposition = NSURLSessionAuthChallengePerformDefaultHandling;
                    }
                }else {
                    disposition = NSURLSessionAuthChallengePerformDefaultHandling;
                }
            }
    
            if (completionHandler) {
                completionHandler(disposition, credential);
            }
        }
    

    在此挑战之前,您需要向 sharedCredentialStorage 添加凭据。 您可以检查苹果文档,因为它相当简单,但要确保保护空间完全匹配,包括领域。 我希望它可以帮助某人。

    【讨论】:

      【解决方案2】:

      我刚刚使用了以下代码并且工作正常

      AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
      NSURLCredential *credential = [[NSURLCredential alloc] initWithUser:@"Username" password:@"Password" persistence:NSURLCredentialPersistenceForSession];
      [manager setCredential:credential]; 
      

      【讨论】:

        【解决方案3】:

        我最近升级到 AFNetworking-3,不再支持 NSURLConnection。

        当我更改为NSURLSession 时,我遇到了同样的问题。

        谢谢鲍勃,编辑 AFNetworkings AFURLSessionManager.m 对我来说很好。

        但在那之后,我发现了一个更简单的方法,如果设置了self.taskDidReceiveAuthenticationChallenge,我们就不必更改 AFNetworkings 代码。

        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        
        NSURLCredential *myCredential = [[NSURLCredential alloc] initWithUser:@"username" password:@"password" persistence:NSURLCredentialPersistenceForSession];
        
        [manager setTaskDidReceiveAuthenticationChallengeBlock:^NSURLSessionAuthChallengeDisposition(NSURLSession * _Nonnull session, NSURLSessionTask * _Nonnull task, NSURLAuthenticationChallenge * _Nonnull challenge, NSURLCredential *__autoreleasing  _Nullable * _Nullable credential) {
            if (challenge.previousFailureCount == 0) {
                *credential = myCredential;
                return NSURLSessionAuthChallengeUseCredential;
            } else {
                return NSURLSessionAuthChallengePerformDefaultHandling;
            }
        }];
        

        【讨论】:

          【解决方案4】:

          我花了一段时间才弄清楚这一点,但我没有看到任何适用于 AFNetworking 4.0 的解决方案(需要 4.0,因为它删除了已弃用的 UIWebView,如果发现,Apple 现在会拒绝),请执行此操作;

          NSURLCredential *myCredential = [[NSURLCredential alloc] initWithUser:username password:password persistence:NSURLCredentialPersistenceForSession];
          
          [manager setAuthenticationChallengeHandler:^id _Nonnull(NSURLSession * _Nonnull session, NSURLSessionTask * _Nonnull task, NSURLAuthenticationChallenge * _Nonnull challenge, void (^ _Nonnull completionHandler)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable credential)) {
              if (challenge.previousFailureCount == 0) {
                  return myCredential;
              } else {
                  return @(NSURLSessionAuthChallengePerformDefaultHandling);
              }
          }];
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-01-30
            • 2015-10-10
            • 1970-01-01
            • 2023-03-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多