[英]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.