【问题标题】:Client Certificate Authentication with Xamarin iOS使用 Xamarin iOS 进行客户端证书身份验证
【发布时间】: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


【解决方案1】:

你不应该使用句柄而不是类句柄吗?

var identity = new SecIdentity(identityHandle.Handle);

到目前为止,你有什么进展吗?我需要 WkWebView 应用程序的类似代码。

【讨论】:

  • 好地方!我改为使用 Handle 属性,它工作得很好!我已经在 GitHub 上更新了我的代码,以防其他人需要它。不幸的是,我对 WkWebView 一无所知,因此无法帮助您,但非常感谢您的帮助!
猜你喜欢
  • 2012-01-10
  • 2015-10-29
  • 2018-07-05
  • 2019-01-14
  • 1970-01-01
  • 2012-04-24
  • 1970-01-01
  • 1970-01-01
  • 2014-08-23
相关资源
最近更新 更多