マシンに証明書がインストールされていて、それを表示すると、「この証明書に対応する秘密鍵があります」というメッセージが表示されますが、コードでその秘密鍵にアクセスしようとすると、null になります。次のコードを使用して証明書を取得します。
var x509Certificate = GetCertificate(StoreName.My, StoreLocation.LocalMachine, "CN=SomeCert");
どこ:
public X509Certificate2 GetCertificate(string storeName, string storeLocation, string subjectName)
{
var store = new X509Store(getStoreName(storeName), getStoreLocation(storeLocation));
X509Certificate2Collection certificates = null;
store.Open(OpenFlags.ReadOnly);
try
{
X509Certificate2 result = null;
certificates = store.Certificates;
return getCertificateResult(certificates, subjectName, result);
}
finally
{
if (certificates != null)
{
foreach (var cert in certificates)
{
cert.Reset();
}
}
store.Close();
}
}
と:
private static X509Certificate2 getCertificateResult(IEnumerable certificates, string subjectName, X509Certificate2 result)
{
foreach (var cert in certificates.Cast<X509Certificate2>().Where(cert => cert.SubjectName.Name != null && cert.SubjectName.Name.ToLower() == subjectName.ToLower()))
{
if (result != null)
{
throw new ApplicationException(string.Format("There is more than one certificate found for subject Name {0}", subjectName));
}
result = new X509Certificate2(cert);
}
if (result == null)
{
throw new ApplicationException(string.Format("No certificate was found for subject Name {0}", subjectName));
}
return result;
}
証明書は正常に返されますが、秘密鍵にアクセスしようとすると、次のようになります。
x509Certificate.PrivateKey
PrivateKey の値は null です。私は何を間違っていますか?SAML2 リクエストに署名するには、この値が必要です。
注:そこにいくつかの抽象化があることは理解していますが、要点は、証明書を取得する(見つかった)が、秘密鍵がnullであることです。質問への回答を妨げている私の抽象化に関する情報がさらにある場合は、詳細を提供できます。