Pet*_*oun 21 .net c# pfx x509certificate2
我正在构建一个受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];
}
Run Code Online (Sandbox Code Playgroud)
问题是,它没有加载验证所需的私钥.我试图将return语句修改为:
return new X509Certificate2(certCollection[0].Export(X509ContentType.Pfx, "password"));
Run Code Online (Sandbox Code Playgroud)
但是,这会因CryptographicException"Spcecified网络密码不正确"而失败.
编辑: 如果您没有传递密码参数,.Export()方法可以正常工作.
对此有何帮助?
导出时,您提供的密码是您要用于导出文件的密码,它不是源证书的密码.
我不确定你可以用X509Store和密码保护的证书做什么,因为密码应该提供给X509Certificate构造函数,你可以从商店中获取已经实例化的对象.
我想您可以从您想要的证书中获取原始数据,并使用您想要的密码构建一个新数据.例如:
X509Certificate2 cert = new X509Certificate2(certCollection[0].GetRawCertData, password);
Run Code Online (Sandbox Code Playgroud)
我还建议你SecureString在处理密码时尝试使用(但那是一堆不同的蠕虫......)