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

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?

Tagged:

Answers

  • robbitrobbit CNMember Xamurai

    Please show more detail error logs.

  • AndrasVAndrasV Member

    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

  • thirdeyesthirdeyes Member

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

Sign In or Register to comment.