【问题标题】:Using a client certificate with Alamofire 2.0在 Alamofire 2.0 中使用客户端证书
【发布时间】:2015-09-21 20:18:30
【问题描述】:

在 Alamofire 1 和 Swift 1.2 上,我使用以下代码发出请求,提交我自己的客户端证书:

Alamofire.request(.POST, url!, parameters: params, encoding: .JSON)
    .authenticate(usingCredential: credential)
    .responseJSON { (request, response, JSON, error) in
        ...
    }

在 Alamofire 和 Swift 2.0 中,我采用了固定证书并使用了我自己的 Manager 实例。但是服务器报告没有提交有效的证书。

let manager = Manager(
    configuration: NSURLSessionConfiguration.defaultSessionConfiguration(),
    serverTrustPolicyManager: ServerTrustPolicyManager(policies:
        ["my.server.com": ServerTrustPolicy.PinCertificates(certificates: [
            SecCertificateCreateWithData(kCFAllocatorDefault,
                NSData(contentsOfFile: NSBundle.mainBundle().pathForResource("root-ca", ofType: "der")!)!)!
            ],
            validateCertificateChain: true, validateHost: true)
        ])
    )

我做错了什么?我无权访问服务器以查看传入请求的外观。但是,接受以下 cURL 请求:

curl -X POST -d '{"foo": "bar"}' https://my.server.com --cert client.pem --insecure

感谢您的帮助!

【问题讨论】:

    标签: ios swift https swift2 alamofire


    【解决方案1】:

    好吧,上面的代码有效!我试图通过创建一个空白项目并一次添加一个项目来隔离问题,直到找到错误为止。由于我无法重现它,我决定删除我实际项目中的所有 CocoaPods 目录并重建整个工作区。原来这解决了这个问题。 在不删除现有目录的情况下重建工作区显然是不够的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-11
      • 2010-12-11
      • 2012-01-08
      • 1970-01-01
      相关资源
      最近更新 更多