首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用NSURLCredentialPersistenceForSession while request,然后在注销时清除凭据仍然保留证书

使用NSURLCredentialPersistenceForSession while request,然后在注销时清除凭据仍然保留证书
EN

Stack Overflow用户
提问于 2012-03-28 17:25:16
回答 4查看 1K关注 0票数 2

我在登录时在NSURLConnection委托方法的didReceiveAuthenticationChallenge中使用NSURLCredentialPersistenceForSession。现在,当我注销并使用此代码清除存储空间时..

代码语言:javascript
复制
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

EN

回答 4

Stack Overflow用户

发布于 2014-07-21 11:20:03

如果您使用的是NSURLSession,请使会话无效并创建一个新会话,例如:

代码语言:javascript
复制
[self.session invalidateAndCancel];
self.session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:nil];

这将清除会话范围的凭据。

票数 1
EN

Stack Overflow用户

发布于 2014-06-17 00:29:43

如果您使用NSURLCredentialPersistenceForSession创建凭据,则应用程序将存储整个会话的凭据,直到应用程序关闭。

您可以通过以下任一方法解决此问题:

  • 更改url (类似于将'#‘附加到url的末尾)
  • 使用NSURLCredentialPersistenceNone并将凭据与每个nsurlrequest
  • Append身份验证信息一起提供给url (http://username:password@mywebsite.com),而不是使用creds
  • 将身份验证信息添加到请求标头,而不是使用creds

//添加到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

可以删除凭据,但可能需要几分钟时间才能真正删除。但是,我不记得是怎么做到的..它要么按照你在问题中提到的方式完成,要么通过密钥链完成。

票数 0
EN

Stack Overflow用户

发布于 2014-10-24 15:09:42

我遇到了同样的问题,我尝试清除凭据存储,与url关联的cookie,甚至尝试重置会话,但似乎都不起作用,最后我只是求助于在url的末尾添加一个随机查询字符串值,这对我很有效。

代码语言:javascript
复制
// 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,这样就不会有任何缓存的对象与之关联。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9904601

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档