繁体   English   中英

签署凭证 - 带私钥的X509证书 - SAML2

[英]Signing Credentials - X509 Certificate with Private Key - SAML2

我对证书几乎一无所知,因为有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

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

它在目录中创建一个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创建证书的密码中。 总是告诉我密码不正确。

我究竟做错了什么?

我相信makecertpvk2pfx在某种程度上是错误的。 我知道这不是答案,但我找到了这个漂亮的小工具:

PluralSight的自我证书

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

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

另一个testcert.cer没有这个。

现在的代码

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM