NSURLCredential和NSURLConnection [英] NSURLCredential and NSURLConnection

查看:151
本文介绍了NSURLCredential和NSURLConnection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试搜索一种方式来取消设置凭据,当您设置NSURLCredential与NSURLConnection使用NSURLCredentialPersistenceForSession,但我找不到一个工作的解决方案。从NSURLCredentialStorage中删除NSURLCredential只会从存储中删除它,而不是从NSURLConnection缓存中删除它。我试图关闭缓存,它仍然保持它。我需要它是NSURLCredentialPersistenceForSession,因为我不想它上传大数据,然后回来,你需要验证消息,然后使用NSURLConnection进行身份验证,然后重新发送大数据,我只想验证一次,并发送大数据一旦。我有一种方式在通过检查一些属性发送大数据之前进行身份验证,但这不允许我注销或重新请求身份验证。我写一个WebDav客户端,让你明白我在哪里站,我需要取消设置凭据的原因是如果有人在WebDav服务器上有多个帐户,并想要登录到另一个帐户。我试图调查饼干看看它是否设置了东西,但它不是。我知道这只是为了会话,这意味着一旦你退出并重新启动应用程序,你可以登录为其他用户。但这可能会混淆一些人。



对不起,如果上面的消息太长,我只是在做确定我详细解释一切,所以有人可以尝试帮助我一个有效的答案,而不是一个,去这里带我去尝试的东西。



感谢任何帮助,

Gecko先生。



更新:示例代码。

 code> CFHTTPMessageRef message = [self httpMessageFromResponse:response]; 
authentication = CFHTTPAuthenticationCreateFromResponse(kCFAllocatorDefault,message);

CFHTTPMessageRef message = [self httpMessageFromRequest:request];
CFStreamError错误;
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];
}
}
}


解决方案>

我正在使用webdav服务器,并有类似的问题。我在Apple的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在userToCredentialMap中){
NSURLCredential * credential = [userToCredentialMap objectForKey:user];
assert(credential!= nil);
[store removeCredential:credential forProtectionSpace:protectionSpace];
}
}


I have been trying to search for a way to unset the credentials once you set a NSURLCredential with a NSURLConnection that is using NSURLCredentialPersistenceForSession, but I couldn't find a working solution. Removing the NSURLCredential from the NSURLCredentialStorage only removes it from the storage and not from the NSURLConnection cache. I tried turning cache off and it still keeps it. I need it to be NSURLCredentialPersistenceForSession as I don't want it to be uploading the large data then getting back the you need to authenticate message then authenticating with NSURLConnection and then resending the large data, I just want to authenticate once and send the large data once. I have a way to authenticate before sending the large data by checking some properties, but that doesn't allow me to logout or re-ask for authentication. I'm writing a WebDav client just so you understand where I stand, and the reason I need to unset the credentials is if someone has multiple accounts on the WebDav server and wants to login to another account. I tried looking into the cookies to see if it set something there, but it doesn't. I know it's only for the session, which means once your quit and relaunch the application, you can login as the other user. But that may confuse some people. I thought about writing my own Authentication system, but I wouldn't know how long that'll take.

Sorry if the above message is too long, I am just making sure I explain everything in detail so someone can try to help me with a valid answer and not a, go here leading me to something I tried.

Thanks for any help,
Mr. Gecko.

Update: Example code.

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

Update: Code that I tried with removing credentials.

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

解决方案

I'm working with a webdav server and had a similar issue. I found a solution within the sample code for apple's AdvancedURLConnection's project. It appears that the secret is to iterate over the protection spaces and then the credentials for each space. It's probably needlessly over complex but it works for me.

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

这篇关于NSURLCredential和NSURLConnection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆