
问题:我有一个具有特定凭据的NSURLConnection。然后我有一个注销,我清除凭据,保护空间,我删除所有缓存的响应并删除sharedhttpcookiestorage中的所有cookie,但是几秒钟后再次呼叫验证请求,即使是错误的凭据,我仍然使用旧的(已删除)证书
以下是一些代码摘录,其中凭据被删除
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.网络上还有一个博客建议为每个网址添加一个“#”,以便强制重新认证,这有效,但只是不能解决问题,因为我需要指望会话的凭据和使用完全不同的凭据的能力。
这是一个错误还是已知的问题,我该如何真正解决这个问题?
坦白地说:我在这里做错了什么?
这真的让我失望,让我不再继续我的工作。
我非常感谢任何输入!
非常感谢!
解决方法 不幸的是,似乎没有解决这个问题。您可以使用NSURLCredentialPersistenceNone或#技巧,或者您可以定义“connectionShouldUseCredentialStorage”委托方法以返回NO。如果您每次都执行此 *** 作,并且您的应用程序不会持续执行会话的凭据,这将迫使每个请求发生挑战。
对于仅执行最少请求或最终使用会话cookie进行身份验证的应用程序,可能会起作用。
对于正在发送大量请求的应用程序,这些解决方案对每一个请求都产生了401响应,额外的挑战响应可以在数据和性能方面加起来。
如果您可以持续使用会话的凭据存储,直到您需要注销,然后切换到其中一个解决方法,那将是很好的,但这是不可能的。
一旦您为会话存储凭证一次,则会为整个TLS会话缓存。这导致需要等待约10分钟,直到会话消失。
您可以在以下网址阅读更多关于此问题:http://developer.apple.com/library/ios/qa/qa1727/_index.html
该文件提到了一个有限的工作,涉及附加一个’。’到服务器名称的末尾。然而,我一直无法得到这样的工作。
除此之外,这些是我可以想到的解决方案:
1)始终使用NSURLCredentialPersistenceNone& connectionShouldUseCredentialStorage应该生成401s的解决方法。自己添加基本身份验证头到请求。这样可以避免额外的401,同时绕过凭据存储。添加该授权的代码如下所示:
Nsstring *s ; Nsstring *authStr ; s = [Nsstring stringWithFormat:@"%@:%@",user,password] ; s = [YourBase64Encoder base64EnCodingForData:[NSData dataWithBytes:[s UTF8String] length:strlen([s UTF8String])]]; authStr = [Nsstring stringWithFormat:@"Basic %@",s] ; [request setValue:authStr forhttpheaderFIEld:@"Authorization"] ;
我不知道这将如何实现其他身份验证方法,但我认为这是可能的。
2)通知用户问题,并要求他们重新启动应用程序
3)实现您自己的低级套接字的http检索机制,完全绕过CFNetwork。祝你好运:>)
总结以上是内存溢出为你收集整理的objective-c – 不能使用不同的NSURLCredentials进行重新认证(旧的被删除的)全部内容,希望文章能够帮你解决objective-c – 不能使用不同的NSURLCredentials进行重新认证(旧的被删除的)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)