[Original] NSURLSession HTTPS Mutual Authentication, nsurlsessionmutual

Source: Internet
Author: User

[Original] NSURLSession HTTPS Mutual Authentication, nsurlsessionmutual

1. Introduce the <NSURLSessionDelegate> Protocol

2. login verification request

-(void)authenticate{    NSURL *url = [NSURL URLWithString:authAddress];    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];    request.HTTPMethod = @"GET";    NSString *userString = @"name:password";    NSData *userData = [userString dataUsingEncoding:NSUTF8StringEncoding];    NSString *base64String = [userData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];    [request setValue:[NSString stringWithFormat:@"Basic %@",base64String] forHTTPHeaderField:@"Authorization"];        NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {            }];    [task resume];}

3. NSURLSessionDelegate callback

#pragma mark -- NSURLSessionDelegate- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler{    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodClientCertificate])//Client Authentication    {        NSURLCredential *credential = [NSURLCredential credentialWithUser:@"name" password:@"password" persistence:NSURLCredentialPersistenceForSession];        completionHandler(NSURLSessionAuthChallengeUseCredential,credential);    }    else if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])//Server Authentication    {        SecTrustRef serverTrust = challenge.protectionSpace.serverTrust;        SecCertificateRef serverCertificate = SecTrustGetCertificateAtIndex(serverTrust, 0);        NSData *serverData = (__bridge_transfer NSData*)SecCertificateCopyData(serverCertificate);        NSData *localData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cert" ofType:@"cer"]];        if ((!localData) || [serverData isEqualToData:localData])        {            NSURLCredential *credential = [NSURLCredential credentialForTrust:serverTrust];            [challenge.sender useCredential:credential forAuthenticationChallenge:challenge];            completionHandler(NSURLSessionAuthChallengeUseCredential,credential);        }        else        {            completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge,nil);        }    }    else    {        completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge,nil);    }}

Note: NSURLAuthenticationMethodClientCertificate is used for client certificate verification. If you have a p12 certificate, use this certificate for authentication. For more information, see this article. NSURLAuthenticationMethodServerTrust is used for server verification, we need to compare the certificate data obtained by serverTrust, The Challenge returned by the Local Certificate with the server. If it is determined to be the same certificate, it will respond to the challenge. Note that, the Protocol callback will trigger two verification challenges. The verification will be canceled if there are other types of challenges.

 

If you have good experience, I hope you can share it with me ~ I am also learning

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.