AFNetworking与NSURLCredential

我有3种方法,UserLogin,Login和LogoutButtonPressed:

UserLogin:我使用AFNetworking使用NSURLCredential连接到一个Windows Authenticated URL:

-(void)UserLogin:(NSString *)user andPassWordExists:(NSString *)password completionHandler:(void (^)(NSArray *resultsObject, NSError *error))completionHandler { NSURL *url = [NSURL URLWithString:kIP]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; NSURLCredential *credential = [NSURLCredential credentialWithUser:user password:password persistence:NSURLCredentialPersistenceForSession]; [operation setCredential:credential]; [[NSOperationQueue mainQueue] addOperation:operation]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { if (completionHandler) { completionHandler(responseObject, nil); } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { if (completionHandler) { completionHandler(nil, error); } }]; [operation start]; } 

这个方法正在被Login方法调用:

 - (void)Login { NSString *rawString = [self.idTextField text]; NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet]; [self.idTextField setText:[rawString stringByTrimmingCharactersInSet:whitespace]]; [userName UserLogin:self.idTextField.text andPassWordExists:self.passwordTextField.text completionHandler:^(id responseObject, NSError *error) { if (responseObject) { [self.idTextField removeFromSuperview]; [self.passwordTextField removeFromSuperview]; [self.loginButton removeFromSuperview]; self.idTextField = nil; self.passwordTextField = nil; //self.loginButton = nil; [self CreateMenu]; [indicatorView stopAnimating]; [indicatorView removeFromSuperview]; indicatorView = nil; [loadingView removeFromSuperview]; loadingView = nil; }else{ [self CustomAlert:@"Sorry Login Failed, User and/or Passsword Incorrect"]; [indicatorView stopAnimating]; [indicatorView removeFromSuperview]; indicatorView = nil; [loadingView removeFromSuperview]; loadingView = nil; } }]; } 

而我正在尝试用LogoutButtonPressed清除我的会话:

 - (void)LogoutButtonPressed { //@TODO: Fix Logout NSDictionary *credentialsDict = [[NSURLCredentialStorage sharedCredentialStorage] allCredentials]; if ([credentialsDict count] > 0) { NSEnumerator *protectionSpaceEnumerator = [credentialsDict keyEnumerator]; id urlProtectionSpace; while (urlProtectionSpace = [protectionSpaceEnumerator nextObject]) { NSEnumerator *userNameEnumerator = [[credentialsDict objectForKey:urlProtectionSpace] keyEnumerator]; id userNameCred; while (userNameCred = [userNameEnumerator nextObject]) { NSURLCredential *cred = [[credentialsDict objectForKey:urlProtectionSpace] objectForKey:userNameCred]; NSLog(@"cred to be removed: %@", cred); [[NSURLCredentialStorage sharedCredentialStorage] removeCredential:cred forProtectionSpace:urlProtectionSpace]; } } } } 

我从这个例子中得到了这个代码: http : //www.springenwerk.com/2008/11/i-am-currently-building-iphone.html

现在我遇到的问题是,当我触发注销button,然后触发login方法没有证书我仍然可以login,如果我注销,然后等待2 – 3分钟,并没有证书login我无法login。 为什么这样performance呢,就好像信用卡还是保存了一样。 请帮忙。

UPDATE

我已经尝试清除我的LogoutButtonPressed中的caching,Cookie和信誉:

 NSURLCache *sharedCache = [NSURLCache sharedURLCache]; [sharedCache removeAllCachedResponses]; NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; NSArray *cookies = [cookieStorage cookies]; id cookie; for (cookie in cookies) { [cookieStorage deleteCookie:cookie]; } NSDictionary *credentialsDict = [[NSURLCredentialStorage sharedCredentialStorage] allCredentials]; if ([credentialsDict count] > 0) { NSEnumerator *protectionSpaceEnumerator = [credentialsDict keyEnumerator]; id urlProtectionSpace; while (urlProtectionSpace = [protectionSpaceEnumerator nextObject]) { NSEnumerator *userNameEnumerator = [[credentialsDict objectForKey:urlProtectionSpace] keyEnumerator]; id userNameCreds; while (userNameCreds = [userNameEnumerator nextObject]) { NSURLCredential *cred = [[credentialsDict objectForKey:urlProtectionSpace] objectForKey:userNameCreds]; [[NSURLCredentialStorage sharedCredentialStorage] removeCredential:cred forProtectionSpace:urlProtectionSpace]; } } } 

它仍然没有工作。

我也尝试清除AuthorizationHeader和cancellingAllOperations,但仍然没有,我仍然可以login后注销错误或没有信用:

 NSURLCache *sharedCache = [NSURLCache sharedURLCache]; [sharedCache removeAllCachedResponses]; NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; NSArray *cookies = [cookieStorage cookies]; id cookie; for (cookie in cookies) { [cookieStorage deleteCookie:cookie]; } NSDictionary *credentialsDict = [[NSURLCredentialStorage sharedCredentialStorage] allCredentials]; if ([credentialsDict count] > 0) { NSEnumerator *protectionSpaceEnumerator = [credentialsDict keyEnumerator]; id urlProtectionSpace; while (urlProtectionSpace = [protectionSpaceEnumerator nextObject]) { NSEnumerator *userNameEnumerator = [[credentialsDict objectForKey:urlProtectionSpace] keyEnumerator]; id userNameCreds; while (userNameCreds = [userNameEnumerator nextObject]) { NSURLCredential *cred = [[credentialsDict objectForKey:urlProtectionSpace] objectForKey:userNameCreds]; [[NSURLCredentialStorage sharedCredentialStorage] removeCredential:cred forProtectionSpace:urlProtectionSpace]; } } } NSURL *url = [NSURL URLWithString:kIP]; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; AFHTTPRequestSerializer <AFURLRequestSerialization> * requestSerializer = manager.requestSerializer; [requestSerializer clearAuthorizationHeader]; AFHTTPRequestOperationManager *httpClient = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:url]; [[httpClient operationQueue] cancelAllOperations]; 

这个问题可以通过在URL的末尾添加一个随机数来解决:

 -(void)UserLogin:(NSString *)user andPassWordExists:(NSString *)password completionHandler:(void (^)(NSArray *resultsObject, NSError *error))completionHandler { NSInteger randomNumber = arc4random() % 999; NSString *requestURL = [NSString stringWithFormat:@"%@?cache=%ld",kIP,(long)randomNumber]; NSURL *url = [NSURL URLWithString:requestURL]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; NSURLCredential *credential = [NSURLCredential credentialWithUser:user password:password persistence:NSURLCredentialPersistenceForSession]; [operation setCredential:credential]; operation.responseSerializer = [AFJSONResponseSerializer serializer]; [[NSOperationQueue mainQueue] addOperation:operation]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { if (completionHandler) { completionHandler(responseObject, nil); } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { if (completionHandler) { completionHandler(nil, error); } }]; [operation start]; } 

并确保您所调用的所有url末尾都有一个随机数字。

Interesting Posts