Objective c 如何将AF2与摘要身份验证一起使用
我一直在搜索“AFNetworking 2 with Digest Authentication”有一段时间了,没有找到关于它的有用讨论(除了,但不幸的是,它看起来像是针对AFNetworking 1的) 以下是我的无身份验证代码:Objective c 如何将AF2与摘要身份验证一起使用,objective-c,ios7,afnetworking,afnetworking-2,digest-authentication,Objective C,Ios7,Afnetworking,Afnetworking 2,Digest Authentication,我一直在搜索“AFNetworking 2 with Digest Authentication”有一段时间了,没有找到关于它的有用讨论(除了,但不幸的是,它看起来像是针对AFNetworking 1的) 以下是我的无身份验证代码: NSString* apiURL = @"https://api.example.com/WS.asmx"; AFHTTPSessionManager* manager = [AFHTTPSessionManager manager]; manager.reques
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);
}
];
在哪里以及如何消化身份验证代码?这个问题有点老了,但我不得不这样做,我找不到任何内置的方法来处理它。 我使用的解决方案是编辑AFURLSessionManager.m中的AFNetworkings。 基本上,您可以修改此委托方法以支持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添加凭据。
您可以查看apple文档,因为这相当简单,但要确保保护空间完全匹配,包括领域。
我希望它能帮助别人。我只是使用了下面的代码,工作得很好
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSURLCredential *credential = [[NSURLCredential alloc] initWithUser:@"Username" password:@"Password" persistence:NSURLCredentialPersistenceForSession];
[manager setCredential:credential];
我最近升级到AFNetworking-3,不再提供
NSURLConnection
支持
当我切换到NSURLSession
时,我遇到了同样的问题
谢谢你,鲍勃,编辑AFNetworkingsAFURLSessionManager.m
对我来说很好
但在那之后,我找到了一个更简单的方法,如果设置了self.taskdireceptiveauthenticationchallenge
,我们就不必更改网络代码
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;
}
}];
我花了一段时间才弄明白这一点,但我没有看到AFNetworking 4.0的任何解决方案(4.0是必需的,因为它删除了不推荐的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);
}
}];