NSURLCredentialStorage sharedCredentialStorage does not return valid credential

0

I have a problem with the sharedCredentialStorage. It won't return the expected credential, even having all parameters of protection space (host, port,...) the same as the one I have set before.

Code to set the defaultCredential:

NSURLCredential* credential = [NSURLCredential credentialWithUser:[self.username text] password:[self.password text] persistence:NSURLCredentialPersistencePermanent];
NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]initWithHost:@"localhost" port:0 protocol:NSURLProtectionSpaceHTTP realm:nil authenticationMethod:NSURLAuthenticationMethodDefault];
[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credential forProtectionSpace:protectionSpace];

After running this piece of code and going to gdb window, the result for the allCredentials (at sharedCredentialStorage) gives:

(lldb) po [[NSURLCredentialStorage sharedCredentialStorage] allCredentials]
(id) $10 = 0x07546d60 {
    "<NSURLProtectionSpace: 0x7550530>" =     {
        lauro = "<NSURLCredential: 0x7573490>: lauro";
    };
}

That means the credential has been correctly added to the sharedCredentialStorage.

But when the http request operation happens and the authentication challange pops out and this code is ran:

credential = [[NSURLCredentialStorage sharedCredentialStorage] defaultCredentialForProtectionSpace:[challenge protectionSpace]];

Nothing is returned to credential variable. Do you have any clue why is this happening?

The output of the gdb gives:

(lldb) po [[[[NSURLCredentialStorage sharedCredentialStorage] allCredentials] allKeys] objectAtIndex:0]
(id) $13 = 0x0754c1b0 <NSURLProtectionSpace: 0x754c1b0>
(lldb) po [[[[[NSURLCredentialStorage sharedCredentialStorage] allCredentials] allKeys] objectAtIndex:0] host]
// Host for the credential @ sharedCredentialStorage
(id) $14 = 0x0a8b8000 localhost
(lldb) po [[[[[NSURLCredentialStorage sharedCredentialStorage] allCredentials] allKeys] objectAtIndex:0] port]
// Port for the credential @ sharedCredentialStorage (0 matches any)
(id) $15 = 0x00000000 <nil>
(lldb) po [[[[[NSURLCredentialStorage sharedCredentialStorage] allCredentials] allKeys] objectAtIndex:0] realm]
// Realm for the credential @ sharedCredentialStorage (nil matches any)
(id) $16 = 0x00000000 <nil>
(lldb) po [[[[[NSURLCredentialStorage sharedCredentialStorage] allCredentials] allKeys] objectAtIndex:0] authenticationMethod]
// Authentication method for the credential @ sharedCredentialStorage (this gets converted to HTTP basic)
(id) $17 = 0x0159c094 NSURLAuthenticationMethodDefault
(lldb) po [[[[[NSURLCredentialStorage sharedCredentialStorage] allCredentials] allKeys] objectAtIndex:0] protocol]
// Protocol for the credential @ sharedCredentialStorage
(id) $18 = 0x0159b504 http
(lldb) po [[challenge protectionSpace] host]
// Host for the credential @ challange
(id) $19 = 0x0754e6c0 localhost
(lldb) po [[challenge protectionSpace] port]
// Port for the credential @ challange
(id) $20 = 0x00000050 [no Objective-C description available]
// Realm for the credential @ challange
(lldb) po [[challenge protectionSpace] realm]
(id) $21 = 0x01bde844 <object returned empty description>
// Authentication method for the credential @ challange
(lldb) po [[challenge protectionSpace] authenticationMethod]
(id) $22 = 0x0159c094 NSURLAuthenticationMethodDefault

// And finally, the check again to see if we have credentials @ sharedCredentialStorage
(lldb) po [[NSURLCredentialStorage sharedCredentialStorage] allCredentials]
(id) $25 = 0x0756f2b0 {
    "<NSURLProtectionSpace: 0x7574a80>" =     {
        lauro = "<NSURLCredential: 0x7574b00>: lauro";
    };
}

One more thing, even if I use some string for realm (and set it properly also at my REST web service) it does not work.

objective-c
ios
nsurlcredential

1 Answer

0

When you get the challenge the port provided in the protection space is probably 80 (or 443). When you are storing the credential you are giving a port of 0. Set it to the correct port (80 for http, 443 for https, unless you know the server you are connecting to is not using the default ports - your lldv output seems to show port 50?) and try that.

answered on Stack Overflow Jan 23, 2013 by quellish

User contributions licensed under CC BY-SA 3.0