Objective c AFN网络的摘要访问认证
我需要使用摘要身份验证,但在AFNetworking中它不起作用Objective c AFN网络的摘要访问认证,objective-c,afnetworking,Objective C,Afnetworking,我需要使用摘要身份验证,但在AFNetworking中它不起作用 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; NSURLCredential *credential = [[NSURLCredential alloc] initWithUser:@"Username" password:@"Password" persistence:NSURLCredentialPersist
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSURLCredential *credential = [[NSURLCredential alloc] initWithUser:@"Username" password:@"Password" persistence:NSURLCredentialPersistenceForSession];
[manager setCredential:credential];
这是对我有用的东西
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSURLCredential *credential = [[NSURLCredential alloc] initWithUser:@"Username" password:@"Password" persistence:NSURLCredentialPersistenceForSession];
[manager setCredential:credential];
要使用AFURLSessionManager,可以将以下代码用于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;
}];
要使用AFURLSessionManager,可以将以下代码用于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 3上不再有效这在AFNetworking 3上不再有效请参见我对AFNetworking 4.0的回答;请参阅我对AF4.0的回答;