Zin*_*ing 5 objective-c digest-authentication afnetworking ios7 afnetworking-2
我一直在寻找"AFNetworking 2 with Digest Authentication"一段时间,并没有找到有关它的有用讨论(除了这一个,但不幸的是它看起来像是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);
}
];
Run Code Online (Sandbox Code Playgroud)
Digest Auth代码在何处以及如何启动?
这个问题有点老了,但我不得不这样做,但我找不到任何内置的方法来处理它。我使用的解决方案是编辑 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);
}
}
Run Code Online (Sandbox Code Playgroud)
在此挑战之前,您需要将凭证添加到共享凭证存储中。您可以检查苹果文档,因为它相当简单,但请确保保护空间完全匹配,包括领域。我希望它能帮助某人。
| 归档时间: |
|
| 查看次数: |
1709 次 |
| 最近记录: |