I used this code: certificate.pfx located in Resources\Raw folder and also tried on the root of the app.

var cert = new X509Certificate2(@"certificate.pfx", "password");

Exception : System.Security.Cryptography.CryptographicException: 'The system cannot find the file specified.'

How can i fixed it ?

Thanks in advance?

Hello,

You can use X509Certificate2(Byte[], String) to read the certifcate to byte[] like following code.

By the way, please make sure this build action of certificate.pfx is MauiAsset

  public async Task<byte[]> ReadCertificateAsByteArray()
           // Get the stream of the certificate from the Assets folder
           using (var stream = await FileSystem.OpenAppPackageFileAsync("certificate.pfx"))
               if (stream != null)
                   using (var memoryStream = new MemoryStream())
                       // Copy the stream to a MemoryStream
                       stream.CopyTo(memoryStream);
                      // Convert the MemoryStream to byte[]
                       return memoryStream.ToArray();
                   Console.WriteLine("Certificate file not found in the Assets folder.");
       catch (Exception ex)
           Console.WriteLine($"Error reading certificate: {ex.Message}");
      return null;

Then, you can use it in X509Certificate2 like following code.

  var certAsByte = await ReadCertificateAsByteArray();
  var cert = new X509Certificate2(certAsByte, "password");

Best Regards,

Leon Lu

If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.