CredStore Perform Query error

This same issue happens to me and I found that if your API URL does not contain a "/" at the end of URL then iOS does not send "Authorization" value to the server. Due to which you will see a message like posted in question in the console.

So Simply add "/" at the end of URL

https://example.com/api/devices/

This error occurs when trying to retrieve an URLCredential from URLCredentialStorage for an unknown URLProtectionSpace. e.g.

let protectionSpace = URLProtectionSpace.init(host: host, 
                                              port: port, 
                                              protocol: "http", 
                                              realm: nil, 
                                              authenticationMethod: nil)

var credential: URLCredential? = URLCredentialStorage.shared.defaultCredential(for: protectionSpace)

produces

CredStore - performQuery - Error copying matching creds.  Error=-25300, query={
    class = inet;
    "m_Limit" = "m_LimitAll";
    ptcl = http;
    "r_Attributes" = 1;
    srvr = host;
    sync = syna;
}

Give it a credential for the protection space:

let userCredential = URLCredential(user: user, 
                                   password: password, 
                                   persistence: .permanent)

URLCredentialStorage.shared.setDefaultCredential(userCredential, for: protectionSpace)

and the error goes away next time you try to retrieve the credential.

I am a little lost as I am not sure what is causing this, or what CredStore even does. What purpose does CredStore serve in iOS?

Credential storage on iOS allows users to securely store certificate-based or password-based credentials on the device either temporarily or permanently to the keychain.

I suspect that you have some sort of authentication on your backend server and that server is requesting an authentication challenge to your app (for which no credential exists).

It can probably be safely ignored as returning nil from the URLCredentialStorage is a valid response


I'm not sure why do we get this error when perform requests with Alamofire, but if you do API requests with some token in HTTP headers, you maybe don't need credentials store at all. So we can disable it for our request:

let configuration = URLSessionConfiguration.default
configuration.httpAdditionalHeaders = ourHeaders
// disable default credential store
configuration.urlCredentialStorage = nil

let manager = Alamofire.SessionManager(configuration: configuration)
...

No errors after such change.

Tags:

Ios

Xcode9