ios - AF 网络 2.0 : credentials not sent to authentication within 30 seconds of previous request

标签 ios authentication afnetworking afnetworking-2 afhttprequestoperation

我正在使用 AFHTTPRequestOperationManager 从我们的服务器获取一些数据。服务器使用基本身份验证。

当用户登录到我们的应用程序时,我会像这样设置凭据:

manager.credential = [NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession];

然后我提出这样的请求:

[manager GET:address parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    // ...
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    // ...
}];

当我发出请求时,NSURLConnectionDelegate 的方法 connection:willSendRequestForAuthenticationChallenge: 被调用(该方法在 AFURLConnectionOperation 中实现)。

当用户注销应用程序并再次登录时,我会根据用户输入再次设置凭据。但是,如果在上一次成功请求后的 30 秒内发生这种情况,则不会将凭据发送到身份验证(connection:willSendRequestForAuthenticationChallenge: 不会被调用)。这意味着即使新凭据不正确,用户也可以注销然后显然登录,因为它们从未被检查过。

如果我等待至少 30 秒,似乎没有问题(即检查凭据并收到 401 错误)。

我是否需要在用户注销时以某种方式清除凭据,还是我的做法全错了?

最佳答案

我遇到了完全相同的问题,发现与 AFNetworking 的 AFHTTPRequestOperationManager 的方法有联系 shouldUseCredentialStorage默认为 YES
由于我没有找到在注销时清除凭据的方法,我现在所做的就是像这样完全禁用 credentialStorage:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.shouldUseCredentialStorage = NO;

不确定这是否是最佳解决方案,但对我有用。

关于ios - AF 网络 2.0 : credentials not sent to authentication within 30 seconds of previous request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24285435/

相关文章:

iOS:iOS 设备上的崩溃日志存储在哪个具体目录中?

zend-framework - Zend Framework 2 Doctrine ORM 身份验证

authentication - Gitlab Ldap 身份验证设置

iphone - 使用 AFNetworking 下载大文件

ios - 为什么这个 API 在 AFNetworking 中以 "failure"的形式返回它的响应,而当它正是它应该是什么并且状态代码是 200 时?

ios - 自定义 uitableviewcell 不占用父 tableview 容器空间的剩余部分

c# - monotuch UICollectionView 给出空引用错误

ios - UITileLayer 显示忽略伪造的图层大小

authentication - Bios加载过程

ios - 请求 addValue : forHTTPHeaderField gets the token but does not sends it to server in RestKit