Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CngKey System.Security.Cryptography.CryptographicException The system cannot find the file specified on Azure

I try to generate key from this code

CngKey key = CngKey.Import(Convert.FromBase64String(privateKey), CngKeyBlobFormat.Pkcs8PrivateBlob);

it works fine locally but when I deploy in my Azure app service. it gives me this error:

System.Security.Cryptography.CryptographicException: The system cannot find the file specified. at System.Security.Cryptography.NCryptNative.ImportKey(SafeNCryptProviderHandle provider, Byte[] keyBlob, String format) at System.Security.Cryptography.CngKey.Import(Byte[] keyBlob, String curveName, CngKeyBlobFormat format, CngProvider provider)

I add WEBSITE_LOAD_USER_PROFILE In Configuration with value '1' but it didn't make any difference.

Thanks

like image 907
adelahmed Avatar asked Nov 16 '25 09:11

adelahmed


2 Answers

I upgraded the plan service from free to basic with adding WEBSITE_LOAD_USER_PROFILE = 1 in Azure Configuration.

The issue was when the app service was free it use a shared VM but when upgrading my app service pricing into basic it use a private VM.

like image 138
adelahmed Avatar answered Nov 19 '25 00:11

adelahmed


If WEBSITE_LOAD_USER_PROFILE = 1 does not work for you. Here is a workaround for this same issue:

  • Open your Azure App Service (Azure Website) blade in portal.azure.com
  • Go to the Application settings page
  • Scroll to App settings
  • Add a new entry key: WEBSITE_LOAD_CERTIFICATES, and provide a dummy (fake, made-up, randomly-generated) value for it.

Check out the below links for the similar issue for reference:

  • X509Certificate2 on Azure App Services (Azure Websites) since mid-2017?
  • GH issue #16
like image 34
Harshita Singh Avatar answered Nov 18 '25 22:11

Harshita Singh