NSURLCredential and NSURLConnection

I am trying to find a way to cancel credentials as soon as you install NSURLCredential with NSURLConnection using NSURLCredentialPersistenceForSession, but I could not find a working solution. Removing the NSURLCredential from the NSURLCredentialStorage only removes it from the repository and not from the NSURLConnection cache. I tried disabling the cache and it still saves it. I need this to be NSURLCredentialPersistenceForSession, since I do not want it to load big data, then return a message authentication request to you and then authenticate with NSURLConnection and then resend the big data, I just want to authenticate one times and send big data once. I have an authentication method before sending big data by checking some properties,but this does not allow me to log out or re-request authentication. I am writing a WebDav client so that you understand where I am standing, and the reason I need to disable credentials is if someone has several accounts on the WebDav server and you want to log in to another account. I tried to look in the cookies to make sure that there is something there, but it is not. I know this only for the session, which means that after completing the work and restarting the application, you can log in as another user. But it can confuse some people. I was thinking of writing my own authentication system, but I did not know how long it would take.- this is if someone has several accounts on the WebDav server and you want to log in to another account. I tried to look in the cookies to make sure that there is something there, but it is not. I know this only for the session, which means that after completing the work and restarting the application, you can log in as another user. But it can confuse some people. I was thinking of writing my own authentication system, but I did not know how long it would take.- this is if someone has several accounts on the WebDav server and you want to log in to another account. I tried to look in the cookies to make sure that there is something there, but it is not. I know this only for the session, which means that after completing the work and restarting the application, you can log in as another user. But it can confuse some people. I was thinking of writing my own authentication system, but I did not know how long it would take.I was thinking of writing my own authentication system, but I did not know how long it would take.I was thinking of writing my own authentication system, but I did not know how long it would take.

Sorry if the above message is too long, I just made sure to explain everything in detail so that someone could help me with the correct answer, and not go here to bring me to something that I tried.

Thanks for any help,
Gekko.

Update: sample 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: The code I tried with credential deletion.

- (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];
        }
    }
}
+3
source share
1 answer

I am working with a webdav server and have a similar problem. I found a solution in the sample code for the appleURURLConnection project. It seems like the secret is to iterate over the security spaces, and then the credentials for each space. It is probably overly 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];
    }
}
-1
source

Source: https://habr.com/ru/post/1789003/


All Articles