Client Authentication Service-side certificate:
Requires domain name for HTTP configuration path
1: First in the project to pour the service-side certificate Sever.cer,
2. Then set the Afsecuritypolicy
Self.manager = [Afhttprequestoperationmanager manager];
Self.manager.responseSerializer = [[Afhttpresponseserializer alloc] init];
[Self.manager.requestSerializer setvalue:@ "iphone" forhttpheaderfield:@ "Header-platform"];
Self.manager.securityPolicy = [Afsecuritypolicy Policywithpinningmode:afsslpinningmodepublickey];
Self.manager.securityPolicy.allowInvalidCertificates = YES;
Self.manager.securityPolicy.validatesDomainName = NO;
Self.manager.securityPolicy.validatesCertificateChain = NO;
The client changes the certificate in the project and the server-side certificate match
Server-side Authentication client certificate, first import the server certificate CLIENT.P12 into the server-side key store
Then add the following method to the AFURLCONNECTIONOPERATION.M
-(Osstatus) Extractidentity: (cfdataref) Inp12data:(secidentityref*) identity {
Osstatus securityerror = errsecsuccess;
Cfstringref Password = cfstr ("clic1234");
const void *keys[] = {Ksecimportexportpassphrase};
const void *values[] = {password};
Cfdictionaryref options = Cfdictionarycreate (null, keys, values, 1, NULL, NULL);
Cfarrayref items = cfarraycreate (null, 0, 0, NULL);
Securityerror = Secpkcs12import (inp12data, Options, &items);
if (Securityerror = = 0)
{
Cfdictionaryref ident = Cfarraygetvalueatindex (items,0);
const void *tempidentity = NULL;
Tempidentity = Cfdictionarygetvalue (ident, ksecimportitemidentity);
*identity = (secidentityref) tempidentity;
}
if (options) {
Cfrelease (options);
}
return securityerror;
}
Put the AFURLCONNECTIONOPERATION.M in the
-(void) connection: (Nsurlconnection *) connection
Willsendrequestforauthenticationchallenge: (Nsurlauthenticationchallenge *) challenge
Replaced by
-(void) connection: (Nsurlconnection *) connection
Willsendrequestforauthenticationchallenge: (Nsurlauthenticationchallenge *) challenge
{
NSString *thepath = [[NSBundle mainbundle] pathforresource:@ "Client" oftype:@ "P12"];
NSLog (@ "thepath===========%@", thepath);
NSData *pkcs12data = [[NSData alloc] initwithcontentsoffile:thepath];
Cfdataref Inpkcs12data = (__bridge cfdataref) pkcs12data;
Secidentityref identity = NULL;
Extract the ideneity from the certificate
[Self Extractidentity:inpkcs12data:&identity];
Seccertificateref certificate = NULL;
Secidentitycopycertificate (identity, &certificate);
const void *certs[] = {certificate};
Cfarrayref Certarray = cfarraycreate (Kcfallocatordefault, certs, 1, NULL);
Create a credential from the certificate and ideneity, then reply to the challenge with the credential
NSLog (@ "identity=========%@", identity);
Nsurlcredential *credential = [nsurlcredential credentialwithidentity:identity certificates:nil Persistence: Nsurlcredentialpersistencepermanent];
credential = [nsurlcredential credentialwithidentity:identity certificates: (__bridge NSArray*) CertArray Persi Stence:nsurlcredentialpersistencepermanent];
[Challenge.sender usecredential:credential Forauthenticationchallenge:challenge];
}
And then we can do two-way authentication.
Afnetworking HTTPS bidirectional authentication