1

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

adelahmed
  • 47
  • 5

4 Answers4

3

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:

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Harshita Singh
  • 4,590
  • 1
  • 10
  • 13
  • the issue was that the app service pricing plan was free when I upgrade it to basic it works fine. Free plan you have a shared VM but start from basic plan you have your own VM – adelahmed Mar 03 '21 at 10:01
2

This issue is not only on Azure, I had the same issue on my VPS as well and this answer save my life:

X509Certificate Constructor Exception

Cheers, Nick

Nick Hoàng
  • 417
  • 2
  • 6
1

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.

adelahmed
  • 47
  • 5
0

I have spent around 4 days looking for this also. I have found out another reason. When you run VS as administrator, then you automatically get the privileges to read something called "cert user store". However if you run it on another machine or some hosting, where you do not have full privileges, then you might run into this issue as well.

Hope this helps as well.

Eru
  • 332
  • 3
  • 17