我正在尝试关注 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,我希望它能解决问题,我认为这张图片表明我做对了吗?
任何人都可以指导我如何创建一个测试证书来让这段代码正常工作,或者给我指明正确的方向吗?我也不确定“您应该使用 Windows Server 生成证书”是什么意思。这是否意味着 creating a CSR?
提前致谢:)
最佳答案
恕我直言,最好模拟真实世界的场景,因此创建一个 CA 来颁发用于 SAML token 生成的证书。您将了解有关 PKI 和证书的更多信息,解决开发时间而非生产时间的问题。但从技术上讲,不需要这样做。
那么需要做什么:
- 下载XCA
- 在 XCA 中制作一个 CA 证书(它已经有 CA 模板,所以直接使用它)。攻略可见here
- 在 CA 下颁发最终实体证书。攻略可见here
- 将 CA 证书(无私钥)导出到文件
- 导入CA证书到LocalMachine的Trusted Root Store(使用mmc->File->add snapin->certificates->computer account)
- 使用私钥(PKCS#12 格式)导出颁发的终端实体证书。
- 将终端实体证书导入 LocalMachine 中的个人存储(使用 mmc->File->add snapin->certificates->computer account)
- 生成 CRL。攻略可见here
- 将 CRL 放置在您颁发终端实体证书时放置的位置,或将其放置在 LocalMachine 的受信任根存储中
- 然后
X509Certificate2 signingCert = CertificateUtil.GetCertificate(StoreName.My, StoreLocation.LocalMachine, "CN=busta-rpsts.com ");
可能会工作...可能。如果不是,您将必须修改私钥权限(再次是 mmc),以便您的帐户可以使用本地计算机个人存储中的私钥。
关于c# - 如何创建测试签名证书?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33105050/