Create a Self-Signed Certificate in .NET, using an Azure Web Application (ASP.NET MVC 5)

霸气de小男生 提交于 2019-12-12 01:28:51

问题


I have this Helper Method:

CngKeyCreationParameters keyParams = new CngKeyCreationParameters();
keyParams.KeyCreationOptions = CngKeyCreationOptions.None;
keyParams.KeyUsage = CngKeyUsages.Signing;
keyParams.Provider = CngProvider.MicrosoftSoftwareKeyStorageProvider;
keyParams.ExportPolicy = CngExportPolicies.AllowExport;
String newguid = Guid.NewGuid().ToString();
CngKey newKey = CngKey.Create(CngAlgorithm2.Rsa, keyParams);
...

When I debug (my local machine), everything is OK, but in the production environment, the last sentence CngKey newKey = CngKey.Create(CngAlgorithm2.Rsa, keyParams); throws the following exception: "The system cannot find the file specified."

The main idea is to create a Self-Signed certificate (x509) to store it and use it for signing PDF documents.

Update:

StackTrace (using: CngKeyCreationOptions.None):

[CryptographicException: The system cannot find the file specified.
]
   System.Security.Cryptography.NCryptNative.CreatePersistedKey(SafeNCryptProviderHandle provider, String algorithm, String name, CngKeyCreationOptions options) +2244958
   System.Security.Cryptography.CngKey.Create(CngAlgorithm algorithm, String keyName, CngKeyCreationParameters creationParameters) +248

StackTrace (using: CngKeyCreationOptions.MachineKey):

[CryptographicException: Access denied.
]
   System.Security.Cryptography.NCryptNative.FinalizeKey(SafeNCryptKeyHandle key) +2243810
   System.Security.Cryptography.CngKey.Create(CngAlgorithm algorithm, String keyName, CngKeyCreationParameters creationParameters) +266

回答1:


Like vcsjones guessed, Azure Web Apps does not load the user profile by default.

You can enable the user profile by setting the app setting WEBSITE_LOAD_USER_PROFILE = 1 for the site in the portal. That might fix your error



来源:https://stackoverflow.com/questions/31685278/create-a-self-signed-certificate-in-net-using-an-azure-web-application-asp-ne

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!