Tôi có 3 phương pháp, UserLogin, Đăng ký và LogoutButtonPressed:AFNetworking với NSURLCredential
UserLogin: Tôi đang sử dụng AFNetworking để kết nối với một Authenticated URL Windows sử dụng NSURLCredential:
-(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];
}
phương pháp này đã được gọi bằng phương pháp nhập:
- (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;
}
}];
}
Và tôi đang cố gắng để xóa phiên của tôi với 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];
}
}
}
}
Tôi có mã này từ ví dụ này: http://www.springenwerk.com/2008/11/i-am-currently-building-iphone.html
Bây giờ vấn đề của tôi, tôi đang gặp là khi tôi kích hoạt các nút logout và sau đó goto kích hoạt các phương pháp đăng nhập VỚI NO credentials tôi vẫn có thể đăng nhập, Nếu tôi đăng xuất sau đó chờ 2 - 3 phút và đăng nhập với NO CREDENTIALS Tôi không thể đăng nhập. Tại sao nó hành xử theo cách này, nó gần giống như các tín hiệu vẫn được lưu lại. Hãy giúp tôi.
CẬP NHẬT
Tôi đã cố gắng để xóa bộ nhớ cache, cookie và creds bên LogoutButtonPressed tôi:
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];
}
}
}
và nó vẫn không làm việc.
Tôi cũng đã cố gắng thanh toán bù trừ AuthorizationHeader và cancelingAllOperations và vẫn không có gì, tôi vẫn có thể đăng nhập bằng sai hoặc không creds sau khi logout:
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];
Bạn đã thêm điểm ngắt và bước qua mã của bạn trong 'LogoutButtonPressed' chưa? Bạn quan sát điều gì? Có gì khác với những gì bạn mong đợi? –
Vâng tôi có. Tôi có thể thấy các tín hiệu đang bị xóa. nhưng khi tôi đăng nhập lại (không có tín dụng), tôi có thể đăng nhập. Tôi đang mong đợi một lỗi. – user979331
Thú vị. Có lẽ phản ứng đang được lưu trữ? Trong lần thử thứ hai của bạn, bạn có thể xem những gì '[[NSURLCache sharedURLCache] cachedResponseForRequest: request]' trả về? –