首页 > 解决方案 > 在 Windows Server 2012 R2 上加载 asp-net 核心中的客户端证书失败

问题描述

我有一个 ASP.NET Core 2.2 应用程序,它加载客户端证书以在连接到外部 API 时使用。该代码在使用 Visual Studio 在本地运行时运行良好,但在我们的服务器上运行时失败。

我在日志中收到以下错误:

2019-03-12 13:29:12.810 +01:00 [ERR] 连接 ID“0HLL6SPAO039S”,请求 ID“0HLL6SPAO039S:00000001”:应用程序引发了未处理的异常。Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException:Internal.Cryptography.Pal.CertificatePal.FilterPFXStore(Byte[] rawData, SafePasswordHandle password, PfxCertStoreFlags pfxCertStoreFlags) 在 Internal.Cryptography.Pal.CertificatePal.FromBlobOrFile(Byte[] rawData) 的未知错误 (0xc0000225) , String fileName, SafePasswordHandle password, X509KeyStorageFlags keyStorageFlags) at System.Security.Cryptography.X509Certificates.X509Certificate..ctor(String fileName, String password, X509KeyStorageFlags keyStorageFlags) at Geomatic.TinglysningApi.Startup.GetClientCertificate() in C:\dev\TinglysningApi \地理。

加载证书的代码如下所示

    private X509Certificate GetClientCertificate()
    {
        return new X509Certificate2(path, password, X509KeyStorageFlags.EphemeralKeySet);
    }

我也尝试过使用其他 X509KeyStorageFlags 以及它们的组合,但它们都以相同的方式失败。

在服务器上,我尝试将我的应用程序作为 IIS 托管应用程序运行,并尝试使用我自己的用户和具有管理员权限的 cmd 从 cmd 运行它。

标签: c#asp.net-corex509certificatewindows-server-2012client-certificates

解决方案


推荐阅读