2

我使用NSURLConnection的调用Web服务并没有出现在我的钥匙串,我在- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challengeNSURLConnection的委托方法,要求authenticationChallenge没有被解雇?

设置为凭据我删除了这个证书,并添加新的一个钥匙圈当客户端证书,NSURLConnection的仍保持凭证我已经给出并在417状态错误代码(这是200,在我删除旧证书之前)给我。

有没有办法让NSURLConnection强制要求凭证?或者如何关闭现有的SSL连接或身份验证质询凭证。

回答

2

NSURLConnection是变态的野兽,过去几天我也遇到类似的问题。但是我已经找到了解决我的问题的方法,并提出了一些建议,以解决您可能遇到的问题的原因。

TLS

首先有可能是发生了什么,你是TLS层缓存的凭据。这是由于建立TLS连接在计算上是昂贵的[1]。

可能的解决方法是更改​​所使用的DNS名称,例如在字符串末尾添加一个点(。),因为DNS协议接受以点为结尾的字符串作为完全限定的DNS名称。我还看到有人向所有URL请求添加hashtag(#),因此欺骗系统从不查找存储的凭证,而只是启动didRecieveAuthenticationChallenge调用,而不是[2]。

饼干

另一种可能性是,服务器设置,你将需要清除的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]; 
      } 
     } 
    } 
} 

我希望这些会有所帮助。