无法将证书/密钥添加到Xamarin iOS钥匙串中

时间:2018-07-09 16:26:12

标签: xamarin xamarin.ios keychain

我正在尝试使用SecKeyChain将我的证书和私钥添加到iOS钥匙串中。我尝试了SecKeyChain.Add()和SecKeyChain.AddIdentity(),但是第一个返回我SecStatusCode“ Param”,而另一个返回了“ System.InvalidOperationException:Param”。有人可以帮我解决这个问题吗?很难从Xamarin网站上找到有关这些方法的详细文档。

using(NSData crt = NSData.FromFile("client1.p12"))
{
    X509Certificate2 certificate = new X509Certificate2(crt.ToArray(), password);
    var identity = SecIdentity.Import(certificate.Export(X509ContentType.Pkcs12, password), password);

    var record = new SecRecord(SecKind.Certificate);
    record.Label = "client1_crt";
    record.SetValueRef(identity.Certificate);
    SecStatusCode secStatus = SecKeyChain.Add(record);

    SecKeyChain.AddIdentity(identity)
}

我也尝试过这种方式:

using (NSData crt = NSData.FromFile("client1-crt.der")
{
    SecStatusCode secStatus = SecKeyChain.Add(new SecRecord(SecKind.Certificate)
    {
        ApplicationLabel = "client1_crt",
        KeySizeInBits = 512,
        KeyClass = SecKeyClass.Public,
        ValueData = NSData.FromString(crt)                                                                   
    }); 
}

但是secStatus仍然显示“ Param”。

1 个答案:

答案 0 :(得分:1)

问题解决了。原来,我们需要在Entitlements.plist文件中启用钥匙串。详细详细步骤:
https://forums.xamarin.com/discussion/comment/330146#Comment_330146