NetStandard 2.0显然不包含`System.Security.Cryptography.X509Certificates.CertificateRequest'.
那么,在.NETStandard 2.0库中创建X509Certificates是不可能的吗?若否,原因为何?X509Certificates被包括在内,所以这似乎是一个奇怪的排除。
发布于 2019-08-31 12:36:34
.NET标准2.0是在.NET框架4.6.1和4.6.2之间构建的,并且没有.NET框架4.6.2中没有的类型。
在4.7.2版本中,.NET框架中添加了CertificateRequest。
最简单的答案是针对.NET框架或.NET核心,而不是.NET标准,然后类型将变得可用(如果您使用足够高的版本)。或者,除非您使用的是自定义X509SignatureGenerator,否则该类型足够简单,您可以将其与反射绑定。
Type certificateRequestType =
Type.GetType("System.Security.Cryptography.X509Certificates.CertificateRequest");
object request = certificateRequestType.GetConstructor(
new[] { typeof(string), typeof(RSA), typeof(HashAlgorithmName), typeof(RSASignaturePadding) }).Invoke(
new object[] { certDN, key, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1 });
Collection<X509Extension> extensions = (Collection<X509Extension>)
certificateRequestType.GetProperty("CertificateExtensions").GetValue(request);
// add things to the extensions collection
...
DateTimeOffset now = DateTimeOffset.UtcNow;
return (X509Certificate2)certificateRequestType.GetMethod("CreateSelfSigned").Invoke(
new object[] { now.AddMinutes(-5), now.AddDays(30) });https://stackoverflow.com/questions/57734997
复制相似问题