【问题标题】:Create a Self-Signed Certificate in .NET, using an Azure Web Application (ASP.NET MVC 5)使用 Azure Web 应用程序 (ASP.NET MVC 5) 在 .NET 中创建自签名证书
【发布时间】:2015-07-29 06:15:22
【问题描述】:

我有这个辅助方法:

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);抛出如下异常:“系统找不到指定的文件。”

主要思想是创建一个自签名证书 (x509) 来存储它并使用它来签署 PDF 文档。

更新:

StackTrace(使用: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):

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

【问题讨论】:

  • 我们能得到那个异常的堆栈跟踪吗?请提供更多详细信息。
  • 尝试将CngKeyCreationOptions 设置为MachineKey 而不是None。我认为 Azure 将您的服务作为没有用户配置文件的帐户运行,这意味着没有加密存储。您还可能会遇到使用机器密钥集的权限问题 - azure Web 应用程序可能不允许您使用机器密钥存储。
  • 谢谢![CryptographicException:访问被拒绝。 ] System.Security.Cryptography.NCryptNative.FinalizeKey(SafeNCryptKeyHandle key) +2243810 System.Security.Cryptography.CngKey.Create(CngAlgorithm algorithm, String keyName, CngKeyCreationParameters creationParameters) +266

标签: asp.net-mvc certificate azure-web-app-service x509


【解决方案1】:

就像 vcsjones 猜测的那样,Azure Web Apps 默认不加载用户配置文件。

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

【讨论】:

  • 非常感谢! vcsjones 和 Zain!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-29
  • 2019-06-30
  • 2014-02-15
  • 2012-09-23
  • 2013-02-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多