2012-03-28 17 views
2

我在登录时使用NSURLCredentialPersistenceForSessiondidReceiveAuthenticationChallengeNSURLConnection委托方法。 现在,当我退出,并使用此代码清除存储..在请求时使用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

+0

你得到一个解决方案吗?我刚开始考虑自己添加注销功能。 – DBD 2012-08-31 14:35:40

回答

0

如果使用NSURLCredentialPersistenceForSession为整个会话创建的凭证,则应用程序商店凭据,直到应用程序被关闭。

可以解决这个有两种方法:

  • 更改URL(如附加“#”到url的末尾)
  • 使用NSURLCredentialPersistenceNone和提供的凭证与每一个的NSURLRequest
  • 追加AUTH信息到URL(http://username:[email protected]),而不是使用creds
  • 追加AUTH信息到请求头,而不是使用creds

    //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"]; 
    

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

1

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

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

这将清除会话范围凭据。

0

我有同样的问题,我试图清除凭据存储,与网址相关的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所以没有任何与之相关的缓存对象。

0

我面临同样的问题,现在它的工作原理。

因此,搜索你URLRequest和随机数追加到URLRequest

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

NSURL *URLRequest = [NSURL URLWithString:requestURL]; 

而且使:

使用NSURLConnection这个问题可以很容易地通过添加一个随机数的URL年底固定确保你在所调用的所有URL的末尾都有一个随机数字。

相关问题