//cert is an EF Entity and 
//    cert.CertificatePKCS12 is a byte[] with the certificate.

var certificate = new X509Certificate(cert.CertificatePKCS12, "SomePassword");

从我们的数据库加载证书时,在我们的暂存服务器(Windows 2008 R2/IIS7.5)上,我们会遇到以下异常:

System.Security.Cryptography.CryptographicException: An internal error occurred.

   at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
   at System.Security.Cryptography.X509Certificates.X509Utils._LoadCertFromBlob(Byte[] rawData, IntPtr password, UInt32 dwFlags, Boolean persistKeySet, SafeCertContextHandle& pCertCtx)
   at System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromBlob(Byte[] rawData, Object password, X509KeyStorageFlags keyStorageFlags)

NOTE:此问题不会在本地发生(Windows 7/Casini).

非常感谢您的任何见解.

推荐答案

Turns out there's a setting in the IIS Application Pool configuration (Application Pools > Advanced Settings) to load the user profile for the application pool identity user. When set to false, the key containers aren't accessible.

因此只需将Load User Profile选项设置为True

App Pool-> Advanced Settings Screen

.net相关问答推荐

PowerShell中窗体定时器和系统定时器的统一处理

通过交互服务器渲染模式和流渲染的组合防止双重渲染

我的Azure应用服务从哪里获取应用设置?

.NET MAUI ListView - ObservableCollection - 在异步方法期间不更新

AppShell - 返回导航失败,匹配的路由不明确......但我只注册了一次路由

如何在 NET 5 / 6 中使用 Direct3D11CaptureFramePool

如何调试 .NET 运行时中的内部错误?

如何判断 IOException 是否为 Not-Enough-Disk-Space-Exception 类型?

Style 和 ControlTemplate 的区别

来自奥尔森时区的 .NET TimeZoneInfo

如何在 C# 4.0 中使任务进入睡眠状态(或延迟)?

HashSet 是否保留插入顺序?

DateTime.TryParseExact() 拒绝有效格式

什么是 ToString("N0") 格式?

如何在 C# 中以编程方式安装 Windows 服务?

从 C# 中的字符串中删除最后一个字符.优雅的方式?

将月份 int 转换为月份名称

如何修复 .NET Windows 应用程序在启动时崩溃并出现异常代码:0xE0434352?

LINQ 可以与 IEnumerable 一起使用吗?

什么时候使用 Tuple 和 KeyValuePair 比较好?