我在登录时在NSURLConnection委托方法的didReceiveAuthenticationChallenge中使用NSURLCredentialPersistenceForSession。现在,当我注销并使用此代码清除存储空间时..
NSURLCredentialStorage *credentialStorage = [NSURLCredentialStorage sharedCredentialStorage];
NSDictionary *credentialsDicationary = [credentialStorage allCredentials];
NSLog(@"credentialsDicationary..%@",[credentialsDicationary description]);
for (NSURLProtectionSpace *space in [credentialsDicationary allKeys]) {
NSDictionary *spaceDictionary = [credentialsDicationary objectForKey:space];
NSLog(@"spaceDictionary..%@",[spaceDictionary description]);
for (id userName in [spaceDictionary allKeys]) {
NSURLCredential *credential = [spaceDictionary objectForKey:userName];
[credentialStorage removeCredential:credential forProtectionSpace:space];
}
}但是,当我突然再次登录时,恰好在注销之后,登录使用了错误的凭据。请让mw知道如何清除缓存。如果我在大约5秒后重新登录,它会起作用。
提前谢谢..AJ
发布于 2014-07-21 11:20:03
如果您使用的是NSURLSession,请使会话无效并创建一个新会话,例如:
[self.session invalidateAndCancel];
self.session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:nil];这将清除会话范围的凭据。
发布于 2014-06-17 00:29:43
如果您使用NSURLCredentialPersistenceForSession创建凭据,则应用程序将存储整个会话的凭据,直到应用程序关闭。
您可以通过以下任一方法解决此问题:
//添加到forHTTPHeaderField:@"Authorization"];的伪代码: NSString *authString = NSString stringWithFormat:@"%@:%@",self.loginCreds.username,self.loginCreds.password;NSData *stringData = authString dataUsingEncoding:NSUTF8StringEncoding;authString = [NSString stringWithFormat:@"Basic %@",stringData base64EncodedString];[self requestHeader setValue:authString authString:@“Basic%@”,stringData base64EncodedString];[self requestHeader setValue:NSUTF8StringEncoding
可以删除凭据,但可能需要几分钟时间才能真正删除。但是,我不记得是怎么做到的..它要么按照你在问题中提到的方式完成,要么通过密钥链完成。
发布于 2014-10-24 15:09:42
我遇到了同样的问题,我尝试清除凭据存储,与url关联的cookie,甚至尝试重置会话,但似乎都不起作用,最后我只是求助于在url的末尾添加一个随机查询字符串值,这对我很有效。
// Random number calculated.
NSInteger randomNumber = arc4random() % 16;
NSURL* apiURL = [NSURL URLWithString:@"https://localhost/api/"];
[[RKObjectManager sharedManager] getObjectsAtPath:[NSString stringWithFormat:@"%@getUser?randomNumber=%d",apiURL,randomNumber]
parameters:nil
success:successBlock
failure:failureBlock];因此,不要尝试删除当前缓存的凭据(这似乎是不可能的),只需使用“新的”url,这样就不会有任何缓存的对象与之关联。
https://stackoverflow.com/questions/9904601
复制相似问题