c# - 正确地自动创建用于开发的自签名证书

标签 c# .net ssl ssl-certificate

我正在尝试自动创建测试环境,其中一个步骤是创建自签名证书以用于 IIS 上的 SSL 绑定(bind)。它是一个 WPF 应用程序,旨在根据指定的配置设置运行一次以创建所有网站、应用程序、Windows 服务等。

我试过使用 CertEnrollLib (certenroll.dll),但由于某些未知原因,当我在服务器上运行代码时出现以下错误:

enter image description here

当我在我的机器上运行时,一切正常。 我的第一个问题是,有人知道为什么会这样吗?我偶然发现了这个链接:

Problems when compiling and running code that uses CertEnroll with .NET 4.0 and x64

我试过上述解决方案:

To get rid of these compilation errors we changed "Embed Interop Types" to "False" in the Properties of the CERTCLIENTLib and CERTENROLLLib references.

没有用。还尝试按照链接中的说明更改平台:

I still didn't have time to figure out why this happens, but if you compile against x86 platform instead of Any CPU platform (which makes the code to run against x64 platform on x64 systems by default), it will work fine.

但这也不起作用,我得到了同样的错误。我之前也尝试过使用 BouncyCaSTLe,但出现了另一个 COM 错误。


编辑:感谢@CryptoGuy,我把它修好了。而不是使用引用库的类构造函数:

var cert = new CX509CertificateRequestCertificate(); // old, not working
// gets the right class to use with my machine, but not with the server

我用这个替换了它:

IX509CertificateRequestCertificate cert = (IX509CertificateRequestCertificate) Activator.CreateInstance(Type.GetTypeFromProgID("X509Enrollment.CX509CertificateRequestCertificate"));
// gets whatever is the CX509CertificateRequestCertificate implementation is

最佳答案

感谢您确认您的服务器操作系统。问题是 IX509CertificateRequestCertificate2接口(interface)在 Windows Server 2008 中不可用,它是在 Windows 6.1 (Windows 7/Windows Server 2008 R2) 中添加的。 您需要使用标准 IX509CertificateRequestCertificate .从技术上讲,它们是相同的,新界面只是增加了注册网络服务支持(以前的系统不提供)。

关于c# - 正确地自动创建用于开发的自签名证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28721785/

相关文章:

c# - 将 SQL 转换为带计数的 linq 表达式

c# - 为什么foreach调用GetHashCode?

.NET 独立后端和 WebAPI 与否

web-services - 在 Web 应用程序中使用受 SSL 保护的 Web 服务

java - TLS SMTP 邮件 Java 抛出 SSLHandShakeException

c# - 在 C# 中将泛型作为泛型类型参数传递

c# - Microsoft.Office.Interop.Word 程序集版本高于引用版本

c# - 如何在 .NET 中导入使用 sn.exe 生成的 .snk 文件?

c# - 生成动态方法来设置结构的字段而不是使用反射

spring - 嵌入式tomcat Spring boot中的ssl设置