如何在Xamarin.Android中将自签名证书添加到TrustedPeople

时间:2018-07-25 17:12:26

标签: c# xamarin.android .net-standard-2.0

我有以下代码将生成的自签名证书添加到证书存储中:

  try
  {
    StorePermission sp = new StorePermission(PermissionState.Unrestricted);
    sp.Flags = StorePermissionFlags.AllFlags;
    sp.Assert();
    StoreName storeName = StoreName.TrustedPeople;
    StoreLocation storeLocation = StoreLocation.CurrentUser;
    X509Store store = new X509Store(storeName, storeLocation);
    store.Open(OpenFlags.ReadWrite);
    Console.WriteLine($"Cert count: {store.Certificates.Count}");
    //store.AddRange(new X509Certificate2Collection(cert));
    store.Certificates.Add(cert);
    Console.WriteLine($"Cert count: {store.Certificates.Count}");
    store.Close();
  }
  catch (Exception ex)
  {
    throw;
  }

它驻留在.Net Standard库中,并且可以完美地与控制台应用程序一起使用。

我打算将其用于 Xamarin.Android 项目。代码可以运行,但是什么也没做,Count方法之后的Add值保持不变。

这是我在输出中看到的:

I/mono-stdout(18830): Cert count: 1
Cert count: 1
D/Mono    (18830): DllImport searching in: 'libmono-btls-shared' ('./libmono-btls-shared.so').
D/Mono    (18830): Searching for 'mono_btls_x509_cmp'.
D/Mono    (18830): Probing 'mono_btls_x509_cmp'.
D/Mono    (18830): Found as 'mono_btls_x509_cmp'.
The thread 0x16d0 has exited with code 0 (0x0).
Cert count: 1
I/mono-stdout(18830): Cert count: 1

根据此thread,这是它应如何工作的方式,但是用户证书列表在电话上为空。

我应该使用哪种权限/方法将其添加到Android证书存储中?

0 个答案:

没有答案