NET中创建自签名证书

NET中创建自签名证书

本文介绍了使用Azure Web应用程序(ASP.NET MVC 5)在.NET中创建自签名证书的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此辅助方法:

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);
...

当我调试(我的本地计算机)时,一切正常,但是在生产环境中,最后一句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."

主要思想是创建一个自签名证书(x509)来存储它,并将其用于签名PDF文档.

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

更新:

StackTrace(使用:CngKeyCreationOptions.None):

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(使用:CngKeyCreationOptions.MachineKey):

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

推荐答案

就像vcsjones一样,Azure Web Apps默认情况下不会加载用户配置文件.

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

您可以通过为门户网站中的站点设置应用程序设置WEBSITE_LOAD_USER_PROFILE = 1来启用用户配置文件.这可能会解决您的错误

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

这篇关于使用Azure Web应用程序(ASP.NET MVC 5)在.NET中创建自签名证书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 07:57