我一直在搜索 stackoverflow、google、apple 和其他地方。提供的提示看起来很有希望,我实施了它们,但总体上似乎没有工作或得到执行。
问题:我有一个NSURLConnection
特定的凭据。然后我有一个注销,我清除凭据,保护空间,我删除所有缓存的响应并删除所有 cookiesharedHTTPCookieStorage
但几秒钟后再次调用我的身份验证请求时,即使使用错误的凭据,我仍然使用旧的(已删除)凭据
以下是一些代码摘录,其中删除了凭据
NSDictionary *credentialsDict = [[NSURLCredentialStorage sharedCredentialStorage] allCredentials];
if ([credentialsDict count] > 0) {
// the credentialsDict has NSURLProtectionSpace objs as keys and dicts of userName => NSURLCredential
NSEnumerator *protectionSpaceEnumerator = [credentialsDict keyEnumerator];
id urlProtectionSpace;
// iterate over all NSURLProtectionSpaces
while (urlProtectionSpace = [protectionSpaceEnumerator nextObject]) {
NSEnumerator *userNameEnumerator = [[credentialsDict objectForKey:urlProtectionSpace] keyEnumerator];
id userName;
// iterate over all usernames for this protectionspace, which are the keys for the actual NSURLCredentials
while (userName = [userNameEnumerator nextObject]) {
NSURLCredential *cred = [[credentialsDict objectForKey:urlProtectionSpace] objectForKey:userName];
WriteLog(@"Method: switchView removing credential %@",[cred user]);
[[NSURLCredentialStorage sharedCredentialStorage] removeCredential:cred forProtectionSpace:urlProtectionSpace];
}
}
}
然后我删除所有缓存的响应
NSURLCache *sharedCache = [NSURLCache sharedURLCache];
[sharedCache removeAllCachedResponses];
然后我删除所有cookie
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray *cookies = [cookieStorage cookies];
for (NSHTTPCookie *cookie in cookies) {
[cookieStorage deleteCookie:cookie];
NSLog(@"deleted cookie");
}
我也尝试过不使用 cookie 和其他政策
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];
[request setHTTPShouldHandleCookies:NO];
if(self.currentCookies != nil){
[request setAllHTTPHeaderFields:
[NSHTTPCookie requestHeaderFieldsWithCookies:nil]];
}
theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
我也在这里尝试了这个提示,专门存储 cookie 并再次传递它们。http://www.hanspinckaers.com/multiple-nsurlrequests-with-different-cookies。网络上有另一个博客建议在每个 URL 中添加一个“#”以强制执行重新身份验证,这有效但不能解决问题,因为我需要依靠会话的凭据和使用完全不同的凭据的能力。
这是一个错误还是已知问题,我该如何真正解决这个问题......坦率地说:我在这里到底做错了什么?
这真的让我很烦恼,让我无法继续我的工作。
我将不胜感激任何意见!
非常感谢!