AFNetworking avec NSURLCredential

J'ai 3 methods, UserLogin, Login et LogoutButtonPressed:

UserLogin: J'utilise AFNetworking pour me connecter à une URL Windows authentifiée à l'aide de NSURLCredential:

-(void)UserLogin:(NSSsortingng *)user andPassWordExists:(NSSsortingng *)password completionHandler:(void (^)(NSArray *resultsObject, NSError *error))completionHandler { NSURL *url = [NSURL URLWithSsortingng: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]; } 

Cette méthode est appelée par la méthode Login:

 - (void)Login { NSSsortingng *rawSsortingng = [self.idTextField text]; NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet]; [self.idTextField setText:[rawSsortingng ssortingngByTrimmingCharactersInSet: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; } }]; } 

Et j'essaie d'effacer ma session avec le 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]; } } } } 

J'ai reçu ce code de cet exemple: http://www.springenwerk.com/2008/11/i-am-currently-building-iphone.html

Maintenant, mon problème est que lorsque je triggers le button de déconnection et que je triggers la méthode de connection SANS POUVOIRS, je peux toujours me connecter. Si je me déconnecte, attendez 2 à 3 minutes et connectez-vous avec NO CREDENTIALS. Pourquoi se comporte-t-il de cette façon, c'est presque comme les creds sont encore sauvés. S'il vous plaît aider.

METTRE À JOUR

J'ai essayé d'effacer le cache, les cookies et les creds dans mon LogoutButtonPressed:

 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]; } } } 

et ça n'a toujours pas marché.

J'ai également essayé d'effacer AuthorizationHeader et cancelingAllOperations et toujours rien, je suis toujours capable de me connecter avec un mauvais ou aucun creds après la déconnection:

 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 URLWithSsortingng:kIP]; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; AFHTTPRequestSerializer <AFURLRequestSerialization> * requestSerializer = manager.requestSerializer; [requestSerializer clearAuthorizationHeader]; AFHTTPRequestOperationManager *httpClient = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:url]; [[httpClient operationQueue] cancelAllOperations]; 

Ce problème peut être résolu facilement en ajoutant un nombre random à la fin de l'URL:

 -(void)UserLogin:(NSSsortingng *)user andPassWordExists:(NSSsortingng *)password completionHandler:(void (^)(NSArray *resultsObject, NSError *error))completionHandler { NSInteger randomNumber = arc4random() % 999; NSSsortingng *requestURL = [NSSsortingng ssortingngWithFormat:@"%@?cache=%ld",kIP,(long)randomNumber]; NSURL *url = [NSURL URLWithSsortingng: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]; } 

Et assurez-vous d'avoir un nombre random à la fin de toutes les URL que vous appelez.