【问题标题】:Certificate privatekey throwing WindowsCryptographicException on ASP.Net Core证书私钥在 ASP.Net Core 上引发 WindowsCryptographicException
【发布时间】:2019-08-21 12:00:24
【问题描述】:

我最近将我的应用程序从 ASP.Net 重写为 ASP.Net Core,从那时起,我无法读取证书的私钥。

这是执行此操作的代码:

public static byte[] DecryptWithCert([NotNull] X509Certificate2 cert, byte[] payload)
{
    using (var rsaPrivateKey = cert.GetRSAPrivateKey())
    {
        return rsaPrivateKey.Decrypt(payload, RSAEncryptionPadding.OaepSHA256);
    }
}

证书肯定有一个私钥,甚至证书对象也显示了这一点。然而,读起来却抛出了这个异常:

cert.PrivateKey' threw an exception of type 'Internal.Cryptography.CryptoThrowHelper.WindowsCryptographicException

异常信息说:

Keyset couldn't be found

这可能是什么问题?

编辑:

问题似乎是由于 ASP.Net Core 未通过 IIS 运行,并且可能缺少现在访问该证书私钥的权限。

我刚刚使用一个小型控制台应用程序对其进行了测试,该应用程序能够获取 PK,因此证书没有损坏。

但是,我真的不知道我必须添加什么用户才能让我的 Kestrel 托管应用程序读取该证书密钥。

【问题讨论】:

标签: c#


【解决方案1】:

好的,我发现问题了:

我刚刚在 certmgr -> All Tasks -> Manage Private keys 中添加了“网络服务”到用户列表。

我的具体问题是,在这篇文章之前的最初方法中,我也尝试了这个,但不知何故,Windows 仍然希望我用德语输入那个用户,因为我最近从德语切换到了英语操作系统。所以这显然只是一个关于正确命名的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-03
    • 1970-01-01
    • 2016-01-05
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    • 2018-04-25
    • 1970-01-01
    相关资源
    最近更新 更多