【问题标题】:Using NSURLCredentialPersistenceForSession while request and then clearing the credentials on logout still persist the cerdentials在请求时使用 NSURLCredentialPersistenceForSession 然后在注销时清除凭据仍然保留凭据
【发布时间】:2012-03-28 09:25:15
【问题描述】:

我在登录时在didReceiveAuthenticationChallengeNSURLConnection 委托方法中使用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 秒后重新登录,它就会起作用。

提前谢谢.. 阿杰

【问题讨论】:

  • 您找到解决方案了吗?我刚开始考虑自己添加注销功能。

标签: ios ipad


【解决方案1】:

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

[self.session invalidateAndCancel];
self.session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:nil];

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

【讨论】:

    【解决方案2】:

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

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

    • 更改网址(例如在网址末尾附加“#”)
    • 使用 NSURLCredentialPersistenceNone 并为每个 nsurlrequest 提供凭据
    • 将身份验证信息附加到 url (http://username:password@mywebsite.com),而不是使用凭据
    • 将身份验证信息附加到请求标头,而不是使用凭据

      //Pseudo code for appending to header:
      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 forHTTPHeaderField:@"Authorization"];
      

    可以删除凭据,但可能需要几分钟才能真正删除。但是,我不记得它是如何完成的。它要么按照您在问题中提到的方式完成,要么通过钥匙串完成。

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,我尝试清除凭据存储、与 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,这样就没有任何与之关联的缓存对象。

      【讨论】:

        【解决方案4】:

        我遇到了同样的问题,现在可以了。

        使用NSURLConnection 可以通过在 URL 末尾添加一个随机数轻松解决此问题:

        所以,搜索您的URLRequest 并将随机数附加到URLRequest

        NSInteger randomNumber = arc4random() % 999;
        NSString *requestURL = [NSString stringWithFormat:@"%@?cache=%ld",yourURL,(long)randomNumber];
        
        NSURL *URLRequest = [NSURL URLWithString:requestURL];
        

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

        【讨论】:

          猜你喜欢
          • 2021-04-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-06-22
          • 2015-11-18
          • 1970-01-01
          • 2014-09-28
          • 1970-01-01
          相关资源
          最近更新 更多