简体   繁体   中英

Certificate pinning in Alamofire not working

I've added a .der certificate to my project, which is recognised by ServerTrustPolicy.certificatesInBundle() . I'm now creating a Manager which is then used to make API calls:

private class func manager() -> Alamofire.Manager {
    let certificates = ServerTrustPolicy.PinCertificates(
        certificates: ServerTrustPolicy.certificatesInBundle(),
        validateCertificateChain: true,
        validateHost: true
    )

    let serverTrustPolicies: [String: ServerTrustPolicy] = [
        "api.oursite.com": certificates
    ]

    let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()

    let manager = Alamofire.Manager(
        configuration: configuration,
        serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
    )

    return manager
}

Before this, it was just using Alamofire.request(... , and those requests were working perfectly fine, but now it's giving me an error on every call:

Error Domain=NSURLErrorDomain Code=-999 "cancelled"

Am I setting up the manager incorrectly? Perhaps my certificate was created incorrectly, and that's causing this to not work?

Be sure that your manager not deallocated in short time after request. Make it as stored property in custom Manager class for example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM