NSURLConnection、基本身份验证和 Cookie 的问题

发布于 2024-12-05 08:56:05 字数 504 浏览 2 评论 0原文

我发现我进行 REST 调用的服务器会将 cookie 传递到我的 iPhone。它还采用 HTTP 基本身份验证。

我有一个应用程序,您可以在其中更改用于身份验证的帐户,但是我发现更改凭据并不重要,因为从未调用 didReceiveAuthenticationChallenge

我研究了两个潜在的修复方法:

  • 每当凭据更改时手动删除 cookie
  • 设置 [request setHTTPShouldHandleCookies:NO]

我想知道我是否正确理解了这一点。我预计 NSURLRequestReloadIgnoringCacheData 会负责缓存,但事实似乎并非如此。

我该如何解决这个问题?

编辑:我刚刚尝试将shouldHandleCookies设置为NO,但cookie似乎仍然传递到服务器。

I have discovered that the server I make REST calls to passes on cookies to my iPhone. It also employs HTTP Basic Auth.

I have an app where you can change accounts used for the authentication, however I have discovered that changing the credentials doesn't matter since didReceiveAuthenticationChallenge is never called.

I have looked into two potential fixes:

  • removing the cookies manually whenever credentials are changed
  • setting [request setHTTPShouldHandleCookies:NO]

I wonder if I'm understanding this correctly. I expected that NSURLRequestReloadIgnoringCacheData would take care of caching, but it doesn't seem to be the case.

How can I resolve this?

EDIT: I've just tried setting shouldHandleCookies to NO, but it seems that the cookies are still passed on to the server.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梅窗月明清似水 2024-12-12 08:56:06

安全 Swift:

func clearCookies(forURL URL: NSURL) -> Void {
    let cookieStorage = NSHTTPCookieStorage.sharedHTTPCookieStorage()
    let cookies = cookieStorage.cookiesForURL(URL) ?? []
    for cookie in cookies {
        print("Deleting cookie for domain: \(cookie.domain)")
        cookieStorage.deleteCookie(cookie)
    }
}

如果您想接收 String ,您始终可以使用 flatMap 可失败的 NSURL 初始值设定项:

let cookies = NSURL(string: string).flatMap(cookieStorage.cookiesForURL) ?? []

Safe Swift:

func clearCookies(forURL URL: NSURL) -> Void {
    let cookieStorage = NSHTTPCookieStorage.sharedHTTPCookieStorage()
    let cookies = cookieStorage.cookiesForURL(URL) ?? []
    for cookie in cookies {
        print("Deleting cookie for domain: \(cookie.domain)")
        cookieStorage.deleteCookie(cookie)
    }
}

If you want to receive a String you can always flatMap the failable NSURL initializer:

let cookies = NSURL(string: string).flatMap(cookieStorage.cookiesForURL) ?? []
隱形的亼 2024-12-12 08:56:05

罗布,你说得很对,这似乎确实有问题。在某些情况下,会设置 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];
    }
  }

看看这个问题以了解更多信息
didReceiveAuthenticationChallenge 仅在 iPhone 上被调用一次

Rob, you are quite right, there does seem to be a problem with this. Cookies are set in some cases that keep the old auth credentials persisted. Others have suggested you may need to clear the cookies like so, and this solved the problem for me:

 - (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];
    }
  }

Take a look at this question for more
didReceiveAuthenticationChallenge getting called only once iPhone

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文