2

我使用 NSURLConnection 调用 Web 服务,并且我的钥匙串中存在一个客户端证书,我将其设置为- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge

在我删除此证书并向钥匙串添加新证书后,NSURLConnection 仍然保留我已经提供的凭据,并在我删除旧证书之前返回 417 状态错误代码,即 200。

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

4

1 回答 1

2

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

TLS

首先,发生在您身上的可能是缓存凭证的 TLS 层。这是因为建立 TLS 连接 [ 1 ] 的计算成本很高。

可能的解决方案是更改您正在使用的 DNS 名称,例如通过在字符串末尾添加一个点 (.),因为 DNS 协议接受以点结尾的字符串作为完全限定的 DNS 名称。我还看到人们向所有 URL 请求添加井号 (#),从而欺骗系统从不查找存储的凭据,而只是发起 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];
          }
      }
   }
}

我希望这些会有所帮助。

于 2012-07-23T16:10:32.623 回答