【问题标题】:NSURLConnection's delegate method, asking for authenticationChallenge is not getting fired?NSURLConnection 的委托方法,要求 authenticationChallenge 没有被解雇?
【发布时间】:2012-07-11 23:44:34
【问题描述】:
我使用 NSURLConnection 调用 web 服务,并且我的钥匙串中存在一个客户端证书,我将其设置为 - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 中的凭据
在我删除此证书并向钥匙串添加新证书后,NSURLConnection 仍然保留我已经提供的凭据,并在我删除旧证书之前返回 417 状态错误代码,即 200。
有没有办法让 NSURLConnection 强制要求凭据。?或者如何关闭现有的 SSL 连接或身份验证质询的凭据。
【问题讨论】:
标签:
ios
certificate
nsurlconnection
ssl-certificate
nsurlconnectiondelegate
【解决方案1】:
NSURLConnection 是善变的野兽,过去几天我一直遇到类似的问题。但我找到了解决我的问题的方法,并提出了一些建议,说明您遇到的问题可能是什么原因。
TLS
首先,发生在您身上的事情有可能是 TLS 层缓存凭据。这是因为建立 TLS 连接的计算成本很高 [1]。
可能的解决方案是更改您正在使用的 DNS 名称,例如在字符串末尾添加一个点 (.),因为 DNS 协议接受以点结尾的字符串作为完全限定的 DNS 名称。我还看到有人在所有 URL 请求中添加标签 (#),从而欺骗系统从不查找存储的凭据,而只是启动 didRecieveAuthenticationChallenge 调用 [2]。
Cookies
另一种可能性是服务器正在设置您需要清除的 cookie。您可以通过执行以下操作来做到这一点:
-(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
现在可能是凭据仍存储在sharedCredentialStorage 中,因此应通过执行以下操作将其删除:
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];
}
}
}
}
我希望这些会有所帮助。