3

I am connecting to server using NSURLConnection. The server asks for basic authentication for which I am using the delegate methods :-didReceiveAuthenticationChallenge. But this gets called only once. If I change password to some different value, then this delegate methods does not get called and it takes my login as successful?

Any kind of help will be appreciated.

Thanks.

rptwsthi
  • 10,094
  • 10
  • 68
  • 109
SoftProdigy
  • 41
  • 1
  • 3

4 Answers4

6

Solved!

It turns out that NSURLConnection was actually behaving correctly - that is, didReceiveAuthenticationChallenge: was being called for every authentication challenge.

The problem was that the server was not sending challenges after the first one. This turned out to be because the server was setting a cookie.

You can force a new challenge by simply deleting the cookie. Because there are no other useful cookies for this server, I just delete all of them:

- (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];
    }
}
Ben Challenor
  • 3,365
  • 1
  • 35
  • 35
1

You have to do following steps to close session completely.

  1. Remove all cookies
  2. Remove all credentials
  3. NSURLCredentialPersistence should be NSURLCredentialPersistenceNone
Erwin
  • 4,757
  • 3
  • 31
  • 41
tmatsugaki
  • 11
  • 1
1

Unfortunately, there is currently no easy way of doing what you asked for:

Apple informative explanation: TLS Session Cache

Open Radar bug request: No way to clear TLS Cache with NSURLConnection

HyBRiD
  • 688
  • 4
  • 23
0

NSURLConnection will cache credentials. Here is an approach to find and erase specific credentials (so you are challenged again):

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

Hope that works for you, Steve

Community
  • 1
  • 1
Steve N
  • 2,667
  • 3
  • 30
  • 37
  • [[NSURLCredentialStorage sharedCredentialStorage] allCredentials] is empty for me, so there's nothing to remove. – Ben Challenor Jun 27 '11 at 12:11
  • If I use NSURLCredentialPersistenceForSession instead of NSURLCredentialPersistenceNone they are added to the allCredentials dictionary, but manually removing them doesn't seem to help. – Ben Challenor Jun 27 '11 at 12:47