AFNetworking 的摘要访问身份验证

Digest access authentication for AFNetworking

我需要使用 Digest 身份验证,但是使用 AFNetworking 它不起作用

这是对我有用的

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

要使用 AFNetworking 3.0,以下代码可用于 AFURLSessionManager。

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];     
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

[manager setTaskDidReceiveAuthenticationChallengeBlock:^NSURLSessionAuthChallengeDisposition(NSURLSession * _Nonnull session, NSURLSessionTask * _Nonnull task, NSURLAuthenticationChallenge * _Nonnull challenge, NSURLCredential *__autoreleasing  _Nullable * _Nullable credential) {
    NSString *authenicationMethod = challenge.protectionSpace.authenticationMethod;
    NSLog(@"Receive challenge: %@", authenicationMethod);
    if ([authenicationMethod isEqualToString:NSURLAuthenticationMethodHTTPDigest]) {
        *credential = [NSURLCredential credentialWithUser:@"username" password:@"password" persistence:NSURLCredentialPersistenceForSession];
        return NSURLSessionAuthChallengeUseCredential;
    }
    return NSURLSessionAuthChallengeCancelAuthenticationChallenge;
}];