Alamofire + Kingfisher Https self-signed certificate

Source: Internet
Author: User

Kingfisher verifying a self-signed certificate download HTTPS pictures

Let Downer =

KingfisherManager.shared.downloader

downer.trustedhosts = Set (["Host Address"])

It's so simple.

Alamofire:

The Servertrustpolicy enumeration uses Pincertificates. Case Pincertificates (certificates: [Seccertificate], Validatecertificatechain:bool, Validatehost:bool) incoming self-signed certificate information. Example code: Let Path = bundle.main. Path (forresource: "Selfsigned_pubca.cer", oftype:nil) Let data = NSData (Contentsoffile:pat h!) Let certificates: [seccertificate] = [Data as! Seccertificate] Let policies: [String:servertrustpolicy] = ["172.16.88.230":. Pincertificates (Certificates:certifica TES, Validatecertificatechain:true, Validatehost:true)] "Pass this policies as a parameter into the manager constructor, butLet certificates: [Seccertificate] = [Data as! Seccertificate] But there's a collapse here, not a strong turn. Next scenario:

Define a struct to store authentication-related information

struct Identityandtrust {

var identityref:secidentity

var trust:sectrust

var certarray:anyobject

}

Func customextractidentity (), Identityandtrust {

var identityandtrust:identityandtrust!

var securityerror:osstatus = errsecsuccess

Let cerpath:string? = Bundle.main.path (forresource: "EIDSP", OfType: "P12")!

Print ("cerpath%@", Cerpath as Any)

Let Cerdata = NSData (contentsoffile:cerpath!)!

Print ("cerdata%@", cerdata as Any)

Let key:nsstring = Ksecimportexportpassphrase as NSString

Let options:nsdictionary = [key: ""]//Client certificate Password

var items:cfarray?

Securityerror = Secpkcs12import (cerdata, Options, &items)

Print ("securityerror%@", Securityerror)

if Securityerror = = errsecsuccess {

Let Certitems:cfarray = items as cfarray!;

Let Certitemsarray:array = Certitems as Array

Let Dict:anyobject? = Certitemsarray.first;

If let certentry:dictionary = Dict as? Dictionary<string, anyobject> {

Let Identitypointer:anyobject? = certentry["Identity"];

Let secidentityref:secidentity = Identitypointer as! secidentity!

Let Trustpointer:anyobject? = certentry["Trust"]

Let Trustref:sectrust = Trustpointer as! Sectrust

Let Chainpointer:anyobject? = certentry["Chain"]

Identityandtrust = Identityandtrust (Identityref:secidentityref,

Trust:trustref, certarray:chainpointer!)

}

}

return identityandtrust;

}

Func Httpsmanager () {

Manager?. Delegate.sessiondidreceivechallenge = {session, challenge in

Print ("Challenge.protectionSpace.authenticationMethod!!!!!!!!!!! %@ ", Challenge.protectionSpace.authenticationMethod)

If Challenge.protectionSpace.authenticationMethod

= = Nsurlauthenticationmethodservertrust

{print ("Server Authentication! ")

Let credential = urlcredential (trust:challenge.protectionspace.servertrust!)

Return (. Usecredential, credential)

}

else if Challenge.protectionSpace.authenticationMethod

= = Nsurlauthenticationmethodclientcertificate {

Print ("Client certificate authentication!") ")

Let Identityandtrust:identityandtrust = customextractidentity ();

Let urlcredential:urlcredential = Urlcredential (

Identity:identityAndTrust.identityRef,

Certificates:identityAndTrust.certArray as? [Anyobject],

Persistence:URLCredential.Persistence.forSession);

Return (. usecredential, urlcredential);

}

else {

Print ("Other cases (not accepted)")

Return (. Cancelauthenticationchallenge, nil)

}

}

}

Put Httpsmanager () This method in the tool class every request is called once, I know that I am a stupid method if there is a way to do well, please inform!



Alamofire + Kingfisher Https self-signed certificate

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.