【发布时间】:2015-10-20 20:05:19
【问题描述】:
我已经成功地通过 .Net HttpWebRequest 类使用客户端证书,但我现在正在尝试更新 ModernHttpClient 以支持客户端证书,因此它使用更高效的 iOS 库来处理请求。
我已修改 NSUrlSessionHandler 中的 DidReceiveChallenge 函数以检查 ClientCertificate 挑战,并尝试跟踪 https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/AuthenticationChallenges.html 上的信息并将其转换为与 Xamarin iOS 一起使用。代码正确编译并且单步执行不会导致打印出任何错误,但最终我仍然收到System.Net.WebException 抛出的消息“服务器“xxx.example.com”需要客户端证书”。
我的代码在下面...
public override void DidReceiveChallenge(NSUrlSession session, NSUrlSessionTask task, NSUrlAuthenticationChallenge challenge, Action<NSUrlSessionAuthChallengeDisposition, NSUrlCredential> completionHandler)
{
...snip...
if (challenge.ProtectionSpace.AuthenticationMethod == NSUrlProtectionSpace.AuthenticationMethodClientCertificate)
{
Console.WriteLine("Client Cert!");
var password = "xxxxx";
var options = NSDictionary.FromObjectAndKey(NSObject.FromObject(password), SecImportExport.Passphrase);
var path = Path.Combine(NSBundle.MainBundle.BundlePath, "Content", "client.p12");
var certData = File.ReadAllBytes(path);
NSDictionary[] importResult;
X509Certificate cert = new X509Certificate(certData, password);
SecStatusCode statusCode = SecImportExport.ImportPkcs12(certData, options, out importResult);
var identityHandle = importResult[0][SecImportExport.Identity];
var identity = new SecIdentity(identityHandle.ClassHandle);
var certificate = new SecCertificate(cert.GetRawCertData());
SecCertificate[] certificates = { certificate };
NSUrlCredential credential = NSUrlCredential.FromIdentityCertificatesPersistance(identity, certificates, NSUrlCredentialPersistence.ForSession);
completionHandler(NSUrlSessionAuthChallengeDisposition.UseCredential, credential);
return;
}
...snip...
}
如果您想试用,我已将我的更改提交到 GitHub (https://github.com/MrsKensington/ModernHttpClient)。顺便说一句,测试客户端证书保护的 Web 服务器绝对没有任何价值(只有一个 .txt 文件),并且证书和密码对于这个 Web 服务器是唯一的,所以我毫不犹豫地与世界分享。
提前感谢您的帮助,
肯辛顿夫人
【问题讨论】:
-
很好的问题@Mrs,您能帮我解决以下问题吗: 1. 如何从IIS 生成P12 文件?可以是 .pfx 还是 .cer?
标签: ios ssl xamarin xamarin.ios client-certificates