3

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

抱歉,如果上述消息太长,我只是确保我详细解释了所有内容,以便有人可以尝试帮助我提供有效的答案,而不是一个,去这里引导我尝试我尝试过的事情。

谢谢你的帮助,
壁虎先生。

更新:示例代码。

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);

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

- (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];
        }
    }
}
4

1 回答 1

-1

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

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];
    }
}
于 2011-02-15T19:36:59.953 回答