从磁盘加载X509证书 .Net Core

24

我使用 OpenSSL 创建了一个 X509 证书。我尝试在 .NET Core 2.0 中使用 X509Certificate2 类的 Import 方法来加载它。

var cert = new X509Certificate2();
cert.Import(_path);

但是会抛出以下异常:

System.PlatformNotSupportedException : X509Certificate is immutable on this 
platform. Use the equivalent constructor instead.

我应该使用哪个构造函数/从磁盘加载证书的正确方式是什么?


你尝试过使用新的X509Certificate2(certificateAsByteArray)吗?可以查看一下 .net core import not implemented 的代码:https://github.com/dotnet/corefx/blob/4617dd43596aabb0a109049dcea124fae5ba743e/src/System.Security.Cryptography.X509Certificates/src/System/Security/Cryptography/X509Certificates/X509Certificate2.cs - RaniDevpr
1个回答

48

您可以使用

var x509 = new X509Certificate2(File.ReadAllBytes(_path));

@user1034912 很抱歉听到这个消息。您使用的运行时版本是什么?您是否在PFX中包含了私钥,还是它是短暂的? - Ivan Zaruba

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接