3

I'm running into an issue where I can't retrieve a credential from NSURLCredentialStorage, even if I try this immediately after saving.

The credential is properly formatted and is accepted by the server, it is set to be permanent. I have debug code to print and confirm that the credential has identity and certificate. However, when I attempt to retrieve it from credential storage, the debug prints all values as Null, while the credential itself is not null.

How do I properly retrieve credentials from NSURLCredentialStorage?

-(void)saveCredential:(NSURLCredential*)credential forProtectionSpace:(NSURLProtectionSpace*)protectionSpace
{

    CLog(@"ATTEMPTING SAVE:");
    [self debugCredential:credential]; //credential is properly formatted

    [[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credential
                                          forProtectionSpace:protectionSpace];


    NSURLCredential* retrievedTest = [[NSURLCredentialStorage sharedCredentialStorage]defaultCredentialForProtectionSpace:protectionSpace];

    CLog(@"ATTEMPTING RETRIEVAL:");
    [self debugCredential:retrievedTest]; //all values are null, credential is not null

}

-(void)debugCredential:(NSURLCredential*) credential
{

    CLog(@"DEBUG Credential: [%@]",credential);
    CLog(@"credential identity: %@", credential.identity);
    CLog(@"credential cert: %@", credential.certificates);
    CLog(@"credential persistence: %lu", (unsigned long)credential.persistence);
}

Here are the logs before and after save

//before save
2016-02-17 16:24:19.669 GWNMobile[2925:716139] DEBUG Credential: [<NSURLCredential: 0x16ff6c00>: (null)]
2016-02-17 16:24:19.670 GWNMobile[2925:716139] credential identity: <SecIdentityRef: 0x16e24890>
2016-02-17 16:24:19.670 GWNMobile[2925:716139] credential cert: (
    "<cert(0x16f9c750) s: [Name] i: [Issuer]>"
)
2016-02-17 16:24:19.670 GWNMobile[2925:716139] credential persistence: 2

//after retrieval:
2016-02-17 16:24:19.670 GWNMobile[2925:716139] ==========================
2016-02-17 16:24:19.691 GWNMobile[2925:716139] ATTEMPTING RETRIEVAL:
2016-02-17 16:24:19.691 GWNMobile[2925:716139] DEBUG Credential: [(null)]
2016-02-17 16:24:19.691 GWNMobile[2925:716139] credential identity: (null)
2016-02-17 16:24:19.691 GWNMobile[2925:716139] credential cert: (null)
2016-02-17 16:24:19.691 GWNMobile[2925:716139] credential persistence: 0
Alex Stone
  • 46,408
  • 55
  • 231
  • 407
  • How are you creating the credential? – dgatwood May 03 '16 at 07:18
  • Credential storage is, IIRC, asynchronous, so it may not be there until your run loop has run a couple of times. Beyond that, I'm not sure how to answer that without knowing more about the credential and whether it is appropriate for the protection space. – dgatwood Oct 09 '17 at 20:10

0 Answers0