[英]Signing Credentials - X509 Certificate with Private Key - SAML2
我对证书几乎一无所知,因为有CA
, public
和private key
,而且我正在学习。 我正在使用SAML 2
创建一个SSO登录,我已经使用了添加<Signature>
元素的例外。
创建证书:
我在目录中有一个makecert.exe
和pvk2pfx.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
创建证书的密码中。 它总是告诉我密码不正确。
我究竟做错了什么?
我相信makecert
和pvk2pfx
在某种程度上是错误的。 我知道这不是答案,但我找到了这个漂亮的小工具:
基本上做了同样的事情,但当我查看证书时它有:
您有一个与此证书对应的私钥
另一个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.