我嘗試使用下面的代碼使用自簽名證書:專用密鑰扔類型System.Security.Cryptography.CryptographicException的異常
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'
但仍然有問題!
來自異常的信息是什麼? – bartonjs
@bartonjs消息:「指定的提供程序類型無效。\ r \ n」 來源:「mscorlib」 –
請運行'certutil -store -silent -v my localhost'並共享結果。 (如果你想要有選擇性,「公鑰算法」和「CERT_KEY_PROV_INFO_PROP_ID(2):」是重要的;但它沒有打印任何內容應該是祕密的) – bartonjs