AFNetworking 的摘要访问身份验证 [关闭]
Posted
技术标签:
【中文标题】AFNetworking 的摘要访问身份验证 [关闭]【英文标题】:Digest access authentication for AFNetworking [closed] 【发布时间】:2015-08-03 09:51:08 【问题描述】:我需要使用 Digest 身份验证,但使用 AFNetworking 它不起作用
【问题讨论】:
【参考方案1】:要使用 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;
];
【讨论】:
查看我对 AFNetworking 4.0 的回答; ***.com/a/63669120/821666【参考方案2】:这对我有用
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSURLCredential *credential = [[NSURLCredential alloc] initWithUser:@"Username" password:@"Password" persistence:NSURLCredentialPersistenceForSession];
[manager setCredential:credential];
【讨论】:
这不再适用于 AFNetworking 3以上是关于AFNetworking 的摘要访问身份验证 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章
AFNetworking 2 使用 AFHTTPRequestOperation 进行身份验证