首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSURLCredential和NSURLConnection

NSURLCredential和NSURLConnection
EN

Stack Overflow用户
提问于 2011-01-31 22:45:27
回答 1查看 3.1K关注 0票数 3

我一直在尝试寻找一种方法,一旦您使用使用NSURLCredentialPersistenceForSession的NSURLCredential设置了一个NSURLConnection,就可以取消设置凭据,但我找不到一个可行的解决方案。从NSURLCredentialStorage中删除NSURLCredential只会将其从存储中删除,而不会从NSURLConnection缓存中删除。我试着关闭缓存,但它仍然保留着它。我需要它是NSURLCredentialPersistenceForSession,因为我不希望它是上传大数据,然后拿回你需要认证的消息,然后与NSURLConnection认证,然后重新发送大数据,我只想认证一次,发送大数据一次。我有一种方法可以在发送大数据之前通过检查一些属性进行身份验证,但这不允许我注销或重新请求身份验证。我正在编写一个WebDav客户端,以便您理解我的立场,我需要取消设置凭据的原因是,如果有人在WebDav服务器上有多个帐户,并且想要登录到另一个帐户。我试着查看cookies,看看它是否在那里设置了什么,但没有,我知道它只用于会话,这意味着一旦你退出并重新启动应用程序,你就可以以其他用户的身份登录。但这可能会让一些人感到困惑。我考虑过编写自己的身份验证系统,但我不知道这需要多长时间。

如果上面的消息太长,我很抱歉,我只是确保我详细地解释了所有的事情,以便有人可以尝试帮助我提供有效的答案,而不是转到这里,引导我找到我尝试过的东西。

谢谢你的帮助

杰科先生。

更新:示例代码。

代码语言:javascript
复制
CFHTTPMessageRef message = [self httpMessageFromResponse:response];
authentication = CFHTTPAuthenticationCreateFromResponse(kCFAllocatorDefault, message);

CFHTTPMessageRef message = [self httpMessageFromRequest:request];
CFStreamError error;
CFHTTPMessageApplyCredentials(message, authentication, (CFStringRef)[credentials user], (CFStringRef)[credentials password], &error);
NSLog(@"%d", error.error); // Returns -1000
CFStringRef value = CFHTTPMessageCopyHeaderFieldValue(message, CFSTR("Authorization"));
NSLog(@"%@", (NSString *)value); // Returns NULL
if (value!=NULL)
 CFRelease(value);

更新:我尝试删除凭据的代码。

代码语言:javascript
复制
- (void)resetCredentials {
    NSURLCredentialStorage *store = [NSURLCredentialStorage sharedCredentialStorage];
    NSDictionary *allCredentials = [store allCredentials];
    NSArray *keys = [allCredentials allKeys];
    for (int i=0; i<[keys count]; i++) {
        NSURLProtectionSpace *protectionSpace = [keys objectAtIndex:i];
        NSDictionary *userToCredentialMap = [store credentialsForProtectionSpace:protectionSpace];
        NSArray *mapKeys = [userToCredentialMap allKeys];
        for (int u=0; u<[mapKeys count]; u++) {
            NSString *user = [mapKeys objectAtIndex:u];
            NSURLCredential *credential = [userToCredentialMap objectForKey:user];
            NSLog(@"%@", credential);
            [store removeCredential:credential forProtectionSpace:protectionSpace];
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2011-02-16 03:37:00

我正在使用webdav服务器,遇到了类似的问题。我在苹果的AdvancedURLConnection项目的示例代码中找到了一个解决方案。看起来秘诀是遍历保护空间,然后遍历每个空间的凭证。它可能没有必要过于复杂,但它对我来说是有效的。

代码语言:javascript
复制
NSURLCredentialStorage *store = [NSURLCredentialStorage sharedCredentialStorage];
assert(store != nil);
for (NSURLProtectionSpace *protectionSpace in [store allCredentials]) {
    NSDictionary *userToCredentialMap = [[NSURLCredentialStorage sharedCredentialStorage] credentialsForProtectionSpace:protectionSpace];
    assert(userToCredentialMap != nil);
    for (NSString *user in userToCredentialMap) {
        NSURLCredential *credential = [userToCredentialMap objectForKey:user];
        assert(credential != nil);
        [store removeCredential:credential forProtectionSpace:protectionSpace];
    }
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4852199

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档