【问题标题】:Signing Credentials - X509 Certificate with Private Key - SAML2签署凭证 - 带有私钥的 X509 证书 - SAML2
【发布时间】:2015-02-25 16:41:41
【问题描述】:

我对证书几乎一无所知,关于有CApublicprivate key,我边走边学。我正在使用 SAML 2 创建一个 SSO 登录,我正在使用添加 <Signature> 元素的例外情况。

创建证书:

我在一个目录中有makecert.exepvk2pfx.exe 的副本。我打开cmd 并输入以下内容:

makecert -r -pe -n "CN=Test Cert" -sky exchange -sv testcert.pvk testcert.cer

弹出一个对话框,要求输入密码并确认密码,我输入。弹出另一个对话框,要求输入密码(我假设这与我之前输入的密码相同,我一直在这样做)。这将在同一目录中创建一个 testcert.cer

然后我将其输入cmd

pvk2pfx -pvk testcert.pvk -spc testcert.cer -pfx testcert.pfx

编辑:它要求我输入密码。我输入的密码 (private key) 与创建 cer 时使用的密码相同。

它在目录中创建一个pfx 文件。


这就是我感到困惑的地方。如果我将cer 文件导入MMC,我可以访问它:

        X509Certificate2 cert = null;

        var store = new X509Store(StoreLocation.CurrentUser);
        store.Open(OpenFlags.ReadOnly);

        var storeCollection = store.Certificates.Find(X509FindType.FindBySubjectName, "Test Cert", false);
        if (storeCollection.Count == 1)
        {
            cert = storeCollection[0];
        }

        if (cert == null)
        {
            throw new ArgumentNullException("Certificate", "No certificate found.");
        }

        store.Close();

但是,当我这样做时,私钥(属性)是null。我读到私钥在pfx 文件中。所以,我没有访问商店,而是这样做:

privateKey 是我使用makecert.exe 创建证书时使用的私钥

X509SigningCredentials clientSigningCredentials = new X509SigningCredentials(
     X509Certificate2(@"c:\directory\testcert.pfx", privateKey, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable));

我收到一个错误:The specified network password is not correct.

我还尝试将pfx 文件导入MMC,而不是cer 文件。它要求我输入密码。我输入了用于使用makecert.exe 创建证书的密码。它总是告诉我密码不正确。

我做错了什么?

【问题讨论】:

  • 您需要加载 pfx 而不是 cer。证书只有公钥。 PFX 是证书和私钥的包。
  • 您应该知道 makecert.exe 已被弃用且不再受支持。
  • @pm100 - 好的,但是当我尝试导入MMC 和/或使用X509SigningCredientals 加载时,为什么private key 总是不正确?
  • 因为 pfx 有密码,所以你必须在阅读时提供它。文档说你必须为 pvk2pfx 提供密码,但你没有在问题中显示它
  • @pm100 - 抱歉 - 忽略了。我提供了与创建 cer 文件时相同的 private key 密码。

标签: c# x509 saml-2.0 pfx makecert


【解决方案1】:

我相信makecertpvk2pfx 是不正确的。我知道这不是答案,但我发现了这个漂亮的小工具:

PluralSight's Self-Cert

基本上做同样的事情,但是当我查看证书时,它有:

您有一个与此证书对应的私钥

其他testcert.cer没有这个。

现在是代码

X509SigningCredentials clientSigningCredentials = new X509SigningCredentials(
 X509Certificate2(@"c:\directory\testcert.pfx", privateKey, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable));

返回没有问题,我的 SAML 断言很好。

【讨论】:

    猜你喜欢
    • 2019-12-06
    • 1970-01-01
    • 2018-07-19
    • 1970-01-01
    • 1970-01-01
    • 2015-01-20
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    相关资源
    最近更新 更多