我正在构建一个受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 new X509Certificate2(certCollection[0].Export(X509ContentType.Pfx, "password"));
然而,这会导致一个 CryptographicException 错误:"指定的网络密码不正确"。
编辑:如果您不传递密码参数,则 .Export() 方法可以正常工作。
这方面有什么帮助吗?