c# - 如何创建测试签名证书?

标签 c# .net certificate saml wif

我正在尝试关注 these instructions使用 WIF 创建 SAML2 token 。

说明说

To supply signing credentials to the SecurityTokenDescriptor, you must first get a reference to the signing certificate as an X509Certificate2 instance. From this you can create a SecurityKeyIdentifier and produce a SigningCredentials instance

提供了以下代码:

X509Certificate2 signingCert = CertificateUtil.GetCertificate(StoreName.My, StoreLocation.LocalMachine, "CN=busta-rpsts.com ");
SecurityKeyIdentifier ski = new SecurityKeyIdentifier(new SecurityKeyIdentifierClause[] { new     X509SecurityToken(signingCert).CreateKeyIdentifierClause<X509SubjectKeyIdentifierClause>() });
X509SigningCredentials signingCreds = new X509SigningCredentials(signingCert, ski);
descriptor.SigningCredentials = signingCreds;

我尝试使用包含主题 key 标识符的 makecert 制作测试证书,但是当我运行通过上述代码制作的任何证书时,我得到以下 System.NotSupportedException :

'X509SecurityToken' does not support 'X509SubjectKeyIdentifierClause' creation.

调用此行时会发生这种情况:

new X509SecurityToken(signingCert).CreateKeyIdentifierClause<X509SubjectKeyIdentifierClause>()

现在我知道说明说...

Note: Certificates created with makecert.exe do not include a subject key identifier and thus cannot be used in this manner. You should produce certificates with Windows Server or purchase a certificate from Verisign or an equivalent provider.

...不过我使用了参数-eku 2.5.29.14,我希望它能解决问题,我认为这张图片表明我做对了吗?

enter image description here

任何人都可以指导我如何创建一个测试证书来让这段代码正常工作,或者给我指明正确的方向吗?我也不确定“您应该使用 Windows Server 生成证书”是什么意思。这是否意味着 creating a CSR?

提前致谢:)

最佳答案

恕我直言,最好模拟真实世界的场景,因此创建一个 CA 来颁发用于 SAML token 生成的证书。您将了解有关 PKI 和证书的更多信息,解决开发时间而非生产时间的问题。但从技术上讲,不需要这样做。

那么需要做什么:

  1. 下载XCA
  2. 在 XCA 中制作一个 CA 证书(它已经有 CA 模板,所以直接使用它)。攻略可见here
  3. 在 CA 下颁发最终实体证书。攻略可见here
  4. 将 CA 证书(无私钥)导出到文件
  5. 导入CA证书到LocalMachine的Trusted Root Store(使用mmc->File->add snapin->certificates->computer account)
  6. 使用私钥(PKCS#12 格式)导出颁发的终端实体证书。
  7. 将终端实体证书导入 LocalMachine 中的个人存储(使用 mmc->File->add snapin->certificates->computer account)
  8. 生成 CRL。攻略可见here
  9. 将 CRL 放置在您颁发终端实体证书时放置的位置,或将其放置在 LocalMachine 的受信任根存储中
  10. 然后 X509Certificate2 signingCert = CertificateUtil.GetCertificate(StoreName.My, StoreLocation.LocalMachine, "CN=busta-rpsts.com "); 可能会工作...可能。如果不是,您将必须修改私钥权限(再次是 mmc),以便您的帐户可以使用本地计算机个人存储中的私钥。

关于c# - 如何创建测试签名证书?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33105050/

相关文章:

c# - 在C#中,接口(interface)可以被实例化吗?

c# - 如何模拟扩展类并实现接口(interface)的对象?

c# - 从 C# 中的字符串中提取 5 位数字的最快方法是什么

c# - 缓存引用数据的线程安全更新

path - 证书基本约束的路径长度

ssl - 在 c# (window) 中实现安全的 webservice 调用

c# - 在 C# 中使用 FakeItEasy 伪造 INI 配置文件设置

asp.net - 加载配置文件时出错 : Failed to map the path '/'

.net - Azure Functions .NET 6 - 从响应 JSON 中删除空字段

c# - Java Force 接受 SSL 证书