gpt4 book ai didi

c# - PrivateKey 引发了 System.Security.Cryptography.CryptographicException 类型的异常

转载 作者:行者123 更新时间:2023-11-30 13:54:14 25 4
gpt4 key购买 nike

我正在尝试使用以下代码使用自签名证书:

X509Certificate2 cert = ToCertificate("CN=localhost");


public static X509Certificate2 ToCertificate(this string subjectName,
StoreName name = StoreName.My,
StoreLocation location = StoreLocation.LocalMachine
)
{
X509Store store = new X509Store(name, location);

store.Open(OpenFlags.ReadOnly);

try
{
var cert = store.Certificates.OfType<X509Certificate2>().FirstOrDefault(c => c.Subject.Equals(subjectName, StringComparison.OrdinalIgnoreCase));

return cert != null ? new X509Certificate2(cert) : null;
}
catch (Exception)
{

throw;
}
finally
{
store.Certificates.OfType<X509Certificate2>().ToList().ForEach(c => c.Reset());
store.Close();
}
}

我收到以下异常:

PrivateKey = 'cert.PrivateKey' threw an exception of type 'System.Security.Cryptography.CryptographicException'

enter image description here

我试过了 this fix , 和 this fix

但还是有问题!

最佳答案

如果您正在调试应用程序,请尝试以管理员身份打开 Visual Studio。它解决了我的问题。

关于c# - PrivateKey 引发了 System.Security.Cryptography.CryptographicException 类型的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45042108/

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