我使用NSURLConnection调用webservice,并且我的密钥链中有一个客户端证书,我将其设置为- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge中的凭据。
在我删除此证书并向密钥链中添加一个新证书后,NSURLConnection仍然维护我已经给出的凭据,并在删除旧证书之前返回417个状态错误代码(即200 )。
有什么办法可以让NSURLConnection有力地要求认证吗?或者如何关闭现有的SSL连接或身份验证挑战的凭据。
发布于 2012-07-23 16:10:32
NSURLConnection是个反复无常的野兽,在过去的几天里,我也遇到了类似的问题。但我已经找到了解决我的问题的办法,并提出了一些建议,这些建议可能是造成像你这样的问题的原因。
TLS
首先,发生在您身上的可能是TLS层缓存凭据。这是因为建立TLS连接[1]在计算上是昂贵的。
对此,可能的解决方案是更改您正在使用的DNS名称,例如添加一个点(.)到字符串的末尾,因为DNS协议接受以点结尾的字符串,作为完全限定的DNS名称。我还看到人们在所有的URL请求中添加了一个hashtag (#),从而欺骗系统永远不要寻找存储的凭据,而只是启动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];
}
}
}
}我希望这些会有所帮助。
https://stackoverflow.com/questions/11448209
复制相似问题