24

我正在构建一个受 ACS 保护的 Azure WCF 服务,该服务将要求客户端通过证书进行身份验证。

我希望客户端(和服务器)从 X509Store 而不是从文件系统加载各自的密码证书。

我正在使用这段代码:

private static X509Certificate2 GetCertificate(string thumbprint)
{
    var certStore = new X509Store(StoreName.My, StoreLocation.LocalMachine);
    certStore.Open(OpenFlags.ReadOnly);

    X509Certificate2Collection certCollection = certStore.Certificates.Find(
        X509FindType.FindByThumbprint,
        thumbprint, false);

    certStore.Close();

    if (certCollection.Count == 0)
    {
        throw new System.Security.SecurityException(string.Format(CultureInfo.InvariantCulture, "No certificate was found for thumbprint {0}", thumbprint));
    }

    return certCollection[0]; 
}

问题是,它没有加载验证所需的私钥。我试图将return语句修改为:

return new X509Certificate2(certCollection[0].Export(X509ContentType.Pfx, "password"));

但是,这会失败并出现 CryptographicException “指定的网络密码不正确”。

编辑: 如果您不传递密码参数, .Export() 方法可以正常工作。

对此有什么帮助吗?

4

3 回答 3

9

导出时,您提供的密码是您要用于导出文件的密码,而不是源证书的密码。

我不确定您可以使用 X509Store 和受密码保护的证书做什么,因为密码应该提供给 X509Certificate 构造函数,并且您可以从存储中获取已经实例化的对象。

认为您可以从您想要的证书中获取原始数据,并使用您想要的密码构建一个新数据。例如:

X509Certificate2 cert = new X509Certificate2(certCollection[0].GetRawCertData, password);

SecureString我还建议您在处理密码时尝试使用(但这是一个不同的蠕虫包......)

于 2012-09-13T04:07:12.127 回答
2

我使用了没有“密码”参数的导出,它没有问题。

于 2012-09-18T00:46:43.773 回答
0

将证书导入证书存储时,我认为必须将密钥标记为“可导出”,否则我认为您无法导出私钥..

在此处输入图像描述

于 2021-12-16T13:00:39.743 回答