Reputation: 673
I use NSURLConnection to call a webservice and there is a client certificate present in my keychain which I set as the credential in - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
After I removed this certificate and adding a new one to keychain, the NSURLConnection still maintains the credential that I give already and gives me back with 417 status error code, which was 200, before I remove the old certificate.
Is there a way to make the NSURLConnection ask for credential, forcefully.? or how can close the existing SSL connection or the authentication challenge's credentials.
Upvotes: 2
Views: 846
Reputation: 2946
NSURLConnection
is fickle beast, and I've been having a similar problem for the past couple of days. But I've found a solution to my problem and a couple of suggestions to what could possibly be the reason for an issue like the one your having.
TLS
First of there is a possibility that what is happening to you is the TLS layer caching the credentials. That is due to it being computationally expensive to establish the TLS connection [1].
Possible solutions to this are to change the DNS name you are using, by for example adding a dot (.) to the end of the string since the DNS protocol accepts a string ending in dots as fully qualified DNS name. I've also seen people adding a hashtag (#) to all URL requests and thus tricking the system to never look for a stored credential but just initiate the didRecieveAuthenticationChallenge
call instead [2].
Cookies
Another possibility is that the server is setting a cookie that you would need to clear. You can do that by doing the following:
-(void)clearCookies:(NSString *)urlString{
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedCookieStorage];
NSURL *url = [[NSURL alloc] initWithString urlString];
NSArray *cookies = [cookieStorage cookiesForURL:tempURL];
for(NSHTTPCookie *cookie in cookies){
//iterate over all cookies and delete them
[cookieStorage deleteCookie:cookie];
}
}
NSURLCredentialStorage
Now it could be that the credentials are still being stored in the sharedCredentialStorage
and thus should be erased by doing the following:
NSURLCredentialStorage *store = [NSURLCredentialStorage sharedCredentialStorage];
if(store !=nil){
for(NSURLProtectionSpace *protectionSpace in [store allCredentials]){
NSDictionary *map = [[NSURLCredentialStorage sharedCredentialStorage]
credentialsForProtectionSpace:protectionSpace];
if(map!=nil){
for(NSString *user in map){
NSURLCredential *cred = [map objectForKey:user];
[store removeCredential:cred forProtectionSpace:protectionSpace];
}
}
}
}
I hope that these will help.
Upvotes: 2