【问题标题】:didReceiveAuthenticationChallenge getting called only once iPhonedidReceiveAuthenticationChallenge 只被调用一次 iPhone
【发布时间】:2011-09-16 08:56:33
【问题描述】:

我正在使用NSURLConnection 连接到服务器。服务器要求我使用委托方法进行基本身份验证:-didReceiveAuthenticationChallenge。但这只会被调用一次。如果我将密码更改为某个不同的值,则不会调用此 delegate 方法并且它认为我的登录成功?

我们将不胜感激。

谢谢。

【问题讨论】:

  • +1,我也有同样的问题。

标签: iphone objective-c nsurlconnection


【解决方案1】:

解决了!

事实证明,NSURLConnection 的行为实际上是正确的——也就是说,didReceiveAuthenticationChallenge: 被调用以应对每个身份验证挑战。

问题是服务器在第一个挑战之后没有发送挑战。原来是因为服务器设置了cookie。

您可以通过简单地删除 cookie 来强制发起新的挑战。由于此服务器没有其他有用的 cookie,我只是将它们全部删除:

- (void)clearCookiesForURL {
    NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    NSArray *cookies = [cookieStorage cookiesForURL:_URL];
    for (NSHTTPCookie *cookie in cookies) {
        NSLog(@"Deleting cookie for domain: %@", [cookie domain]);
        [cookieStorage deleteCookie:cookie];
    }
}

【讨论】:

    【解决方案2】:

    您必须执行以下步骤才能完全关闭会话。

    1. 删除所有 cookie
    2. 删除所有凭据
    3. NSURLCredentialPersistence 应该是 NSURLCredentialPersistenceNone

    【讨论】:

      【解决方案3】:

      很遗憾,目前没有简单的方法可以满足您的要求:

      苹果资料说明: TLS Session Cache

      打开雷达错误请求: No way to clear TLS Cache with NSURLConnection

      【讨论】:

        【解决方案4】:

        NSURLConnection 将缓存凭据。这是一种查找和删除特定凭据的方法(因此您再次受到挑战):

        Is it possible to prevent an NSURLRequest from caching data or remove cached data following a request?

        希望对你有用, 史蒂夫

        【讨论】:

        • [[NSURLCredentialStorage sharedCredentialStorage] allCredentials] 对我来说是空的,所以没有什么可以删除的。
        • 如果我使用 NSURLCredentialPersistenceForSession 而不是 NSURLCredentialPersistenceNone 它们被添加到 allCredentials 字典中,但手动删除它们似乎没有帮助。
        猜你喜欢
        • 2021-01-04
        • 2019-08-23
        • 2017-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多