I have this code:
var file2 = await FileSystem.OpenAppPackageFileAsync("certificado.p12");
using (StreamReader sr = new StreamReader(file2))
var bytes = sr.CurrentEncoding.GetBytes(sr.ReadToEnd());
var x509 = new X509Certificate2(bytes);
It's return this exception:
System.Security.Cryptography.CryptographicException: Unable to decode certificate. ---> System.Security.Cryptography.CryptographicException: `MonoBtlsPkcs12.Import` failed.
at Mono.Btls.MonoBtlsObject.CheckError (System.Boolean ok, System.String callerName) [0x00006] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/System/Mono.Btls/MonoBtlsObject.cs:95
at Mono.Btls.MonoBtlsObject.CheckError (System.Int32 ret, System.String callerName) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/System/Mono.Btls/MonoBtlsObject.cs:103
at Mono.Btls.MonoBtlsPkcs12.Import (System.Byte[] buffer, Microsoft.Win32.SafeHandles.SafePasswordHandle password) [0x0002e] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/System/Mono.Btls/MonoBtlsPkcs12.cs:117
I tried it:
using (StreamReader sr = new StreamReader(file, Encoding.ASCII, true))
byte[] raw = Encoding.ASCII.GetBytes(sr.ReadToEnd());
var x509 = new X509Certificate2(raw, string.Empty, X509KeyStorageFlags.DefaultKeySet);
but return the same
Hi @JessieZhang-MSFT ,
I have this "certificado.p12" archive in my project , i need to create a instance from "new X509Certificate2",
i can read it "var bytes = sr.CurrentEncoding.GetBytes(sr.ReadToEnd())"
but here "var x509 = new X509Certificate2(bytes)" return the error: Unable to decode certificate.
This is on a xamarin.forms project.
Hi @Alexandre Lima ,thank you for posting in Developer Community. I have found you have post an issue about this problem in github.
I am checking with a senior colleague to further investigate your problem. And I’ll get back to you as soon as I have an update. Much appreciated for your patience.
Hi @Alexandre Lima , may I know why you're putting the certificate file in your project? If you're using a client side certificate, it's always suggested to hand your certificate to OS, and get the certificate from OS when you need to use it. Using certificate in mobile OS will have to go native, rather than implement in XF shared project.