locked
How to add a self-signed certificate to TrustedPeople in Xamarin.Android RRS feed

  • Question

  • User371187 posted

    I have the following code to add a generated self-signed certificate to the certificate store:

      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;
      }
    

    It resided in a .Net Standard libray and works flawlessly with a console application.

    I intend to use it with a Xamarin.Android project. The code runs, but it does nothing, the Count value remains the same after the Add method.

    This is what I see on the output:

    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
    

    What permission/method should I use to add it to the Android cert store?

    Thursday, July 26, 2018 7:09 AM

All replies

  • User355640 posted

    Please show more detail error logs.

    Thursday, August 2, 2018 8:27 AM
  • User371187 posted

    There is no error, so I can't provide error logs.

    It also seems that this code can't actually work as there is no implicit certification adding, it must be added by user interaction, see this stackoverflow.com/questions/11657172/how-to-install-ca-certificate-programmatically-on-android-without-user-interacti/32149710

    Thursday, August 2, 2018 8:52 AM
  • User386670 posted

    Hi , is this problem got a solution? Thank you. I'm facing the same issue.

    Tuesday, June 18, 2019 5:08 PM