gpt4 book ai didi

Azure Web App : System. Security.Cryptography.CryptographicException: key 集不存在

转载 作者:行者123 更新时间:2023-12-02 05:23:39 24 4
gpt4 key购买 nike

我的应用程序在创建 X509Certificate2 期间遇到“System.Security.Cryptography.CryptographicException: key 集不存在”异常。

从 Azure Key Vault 读取证书的函数:

 private string GetEncryptSecret(string certConfigName)
{
var kv = new KeyVaultClient(GetToken);
var sec = kv.GetSecretAsync(WebConfigurationManager.AppSettings[certConfigName]).Result;
return sec.Value;
}

如何创建新的 X509Certificate2 对象:

 public X509Certificate2 GetCertificate(CertificatesEnum certificate)
{
switch (certificate)
{
case CertificatesEnum.Accounts:
return new X509Certificate2(
Convert.FromBase64String(GetEncryptSecret(Constants.Magda.Certificates.Accounts)),
string.Empty, X509KeyStorageFlags.MachineKeySet);
}
}

注意到这仅适用于前 3-9 个请求后,我启动了 Azure 远程调试 session ,并看到新的certificate.Privatekey 导致了“System.Security.Cryptography.CryptographyException: key 集不存在”异常。

通过实现等待循环临时修复了此问题,因为在几次重试后,将毫无异常(exception)地创建新证书。

public X509Certificate2 GetCertificate(CertificatesEnum certificate)
{
switch (certificate)
{
case CertificatesEnum.Accounts:
var accountCertRawData = Convert.FromBase64String(GetEncryptSecret(Constants.Magda.Certificates.Accounts));
X509Certificate2 accountCert = null;
for (int i = 0; i < 20; i++)
{
try
{
accountCert= new X509Certificate2(accountCertRawData , string.Empty,
X509KeyStorageFlags.MachineKeySet);
//set variable to test exception
var accountCert = accountCert.PrivateKey;
break;
}
catch (System.Security.Cryptography.CryptographicException)
{
Thread.Sleep(1000 * i);
}
}
return accountCert;
}
}

是否有人对此有适当的解决方案,并且可以解释 Azure Web 应用程序后台发生了什么?

最佳答案

尝试从 Web 应用程序中删除 WEBSITE_LOAD_USER_PROFILE 应用程序设置(如果可用)。

关于Azure Web App : System. Security.Cryptography.CryptographicException: key 集不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49693982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com