2011-06-16 170 views
3

我使用NSURLConnection連接到服務器。服務器要求使用委託方法的基本身份驗證: - didReceiveAuthenticationChallenge。但是這隻會被調用一次。如果我將密碼更改爲某個不同的值,那麼這個delegate方法不會被調用,它會使我的登錄成功?didReceiveAuthenticationChallenge只會被調用一次iPhone

任何形式的幫助將不勝感激。

謝謝。

+0

+1,我有同樣的問題。 – 2011-06-27 12:06:25

回答

0

NSURLConnection將緩存憑證。這是一種方法找到並刪除特定的憑證(所以你再次挑戰):

Is it possible to prevent an NSURLRequest from caching data or remove cached data following a request?

希望,你的作品, 史蒂夫

+0

[[NSURLCredentialStorage sharedCredentialStorage] allCredentials]對我來說是空的,所以沒有東西可以刪除。 – 2011-06-27 12:11:00

+0

如果我使用NSURLCredentialPersistenceForSession而不是NSURLCredentialPersistenceNone,它們被添加到allCredentials字典中,但手動刪除它們似乎沒有幫助。 – 2011-06-27 12:47:47

6

解決了!

事實證明,NSURLConnection實際上行爲正確 - 即每個驗證質詢都會調用didReceiveAuthenticationChallenge:

問題是服務器在第一個問題後沒有發送問題。原來,這是因爲服務器正在設置一個cookie。

您可以通過簡單地刪除cookie來強制新的挑戰。由於沒有其他有用的餅乾此服務器,我只是刪除所有的人:

- (void)clearCookiesForURL { 
    NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; 
    NSArray *cookies = [cookieStorage cookiesForURL:_URL]; 
    for (NSHTTPCookie *cookie in cookies) { 
     NSLog(@"Deleting cookie for domain: %@", [cookie domain]); 
     [cookieStorage deleteCookie:cookie]; 
    } 
} 
1

你必須做以下步驟完全關閉會話。

  1. 刪除所有cookie
  2. 刪除所有憑證
  3. NSURLCredentialPersistence應該NSURLCredentialPersistenceNone
相關問題