如何将AFNetworking 2与摘要式身份验证一起使用

时间:2014-02-26 19:26:43

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

我一直在搜索“AFNetworking 2 with Digest Authentication”一段时间,并没有找到有关它的有用讨论(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代码在何处以及如何启动?

4 个答案:

答案 0 :(得分:2)

这个问题有点陈旧,但我不得不这样做,我找不到任何内置的方法来处理它。 我使用的解决方案是编辑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添加凭据。 您可以检查苹果文档,因为它非常简单,但要确保保护空间完全匹配,包括领域。 我希望它有所帮助。

答案 1 :(得分:2)

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

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

谢谢bob,编辑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;
    }
}];

答案 2 :(得分:0)

让我花点时间解决这个问题,但我没有看到AFNetworking 4.0的任何解决方案(4.0是必需的,因为它删除了Apple现在拒绝的已弃用的UIWebView,如果找到了,则这样做);

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

答案 3 :(得分:-1)

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

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