【原创】NSURLSession HTTPS Mutual Authentication
1.引入<NSURLSessionDelegate>协议
2.登录验证请求
-(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回调
#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, );
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);
}
}
注意:NSURLAuthenticationMethodClientCertificate为客户端证书验证,有p12证书的话需要使用此证书进行认证,方法参考此文章;NSURLAuthenticationMethodServerTrust为服务端验证,我们需要用本地证书与服务端返回的挑战的serverTrust获得的证书数据进行比对,如果判断为同一证书,则响应挑战;特别要注意的是,协议回调会触发两次,分别为以上两种验证挑战,如有其它类型挑战则取消本次验证
各位大神如有好的经验希望分享出来~我也是在学习中
【原创】NSURLSession HTTPS Mutual Authentication的更多相关文章
- [转] An Introduction to Mutual SSL Authentication
1. Introduction Mutual SSL authentication or certificate based mutual authentication refers to two p ...
- 网络服务器之HTTPS服务
import ssl, socket, time if __name__ == "__main__": context = ssl.SSLContext(ssl.PROTOCOL_ ...
- Configuring HTTP and HTTPS
Configuring HTTP and HTTPS .NET Framework (current version) Other Versions WCF services and clie ...
- 服务器 apache配置https,http强制跳转https(搭建http与https共存)
公司linux服务器上的nginx的已经改成https了,现在还剩下一个windows云服务器没配置. 环境 windows wampserver2.5 64位 1.腾讯云申请的ssl 包含三个文件: ...
- xmapp开启https
在开发微信小程序的时候我们需要开启https本地测试,以下我们说明使用xmapp如何开启https访问 1. php中开启ssl 在php的配置文件中把openssl前面的注释去掉, 大概在配置文件的 ...
- sip user Authentication and 401
https://www.vocal.com/sip-2/sip-user-authentication/ https://tools.ietf.org/html/rfc3261 SIP User Au ...
- Maven 搭建spring boot多模块项目(附源码),亲测可以,感谢原创
原创地址:https://segmentfault.com/a/1190000005020589 我的DEMO码云地址,持续添加新功能: https://gitee.com/itbase/Spring ...
- kerberos (https://en.wikipedia.org/wiki/Kerberos_(protocol))
Protocol[edit] Description[edit] The client authenticates itself to the Authentication Server (AS) w ...
- trust an HTTPS connection
https://zh.wikipedia.org/wiki/传输安全协议 SSL协议客户端要收发几个握手信号: 发送一个“ClientHello”消息,内容包括:支持的协议版本,比如TLS1.0版,一 ...
随机推荐
- 求最短路径算法之SPAF算法。
关于求最短路径: 求最短路径的算法有许多种,除了排序外,恐怕是OI界中解决同一类问题算法最多的了.最熟悉的无疑是Dijkstra(不能求又负权边的图),接着是Bellman-Ford,它们都可以求出由 ...
- javascript this指针指向?
前言 理解javascript的指针就需要先了解js的执行环境和作用域!执行环境的定义了变量或函数有权访问的其他数据,决定了它们各自的行为.每个执行环境都有一个与之关联的变量对象,环境中定义的所有的变 ...
- SQL Server 2014 新特性:IO资源调控
谈谈我的微软特约稿:<SQL Server 2014 新特性:IO资源调控> 2014-07-01 10:19 by 听风吹雨, 570 阅读, 16 评论, 收藏, 收藏 一.本文所涉及 ...
- 【分享】SAS统计分析软件学习教程电子书合集下载
SAS是著名的统计分析软件,全称为Statistics Analysis System,最早由北卡罗来纳大学的两位生物统计学研究生编制,并于1976年成立了SAS软件研究所,正式推出了SAS软件. 转 ...
- VS20XX-Add-In插件开发
参考文章: 1:http://www.cnblogs.com/hecool/archive/2013/04/06/3002822.html 2: http://www.cnblogs.com/ande ...
- Jeffrey Richter's Power Threading Library
Jeffrey Richter's Power Threading Library The Power Threading Library consists of a number of classe ...
- map,set的模板[STL]
之前写某题单词查找的时候,有用到set,晚上写codeforces#497 div2 D题有用到map 感觉set,map的功能挺类似的,在此小结: 这里就不具体比较相同点和不同点了,免得看起来乱七八 ...
- [google面试CTCI] 1-6.图像旋转问题
[字符串与数组] Q:Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, wr ...
- ARM备忘
mov: move sub: subtract cmp: compare cmpls: ls--->lower or same, le--->less or equal, hi---> ...
- DevExpress 学习使用之 NavBarControl
TNND,没辙啊,没用过那么高级的玩意儿,暂时也没找到中文的详细帮助,简直就是蚂蚁搬家似的摸索,一点儿点儿来吧. 先是NavBarControl的界面样子,貌似可以通过 PaintStyleKind ...