NSURLConnection、基本身份验证和 Cookie 的问题
我发现我进行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
安全 Swift:
如果您想接收
String
,您始终可以使用flatMap
可失败的NSURL
初始值设定项:Safe Swift:
If you want to receive a
String
you can alwaysflatMap
the failableNSURL
initializer:罗布,你说得很对,这似乎确实有问题。在某些情况下,会设置 Cookie 以保留旧的身份验证凭据。其他人建议您可能需要像这样清除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:
Take a look at this question for more
didReceiveAuthenticationChallenge getting called only once iPhone