C# C加载需要密码的证书
我有下面的代码,当只有一个证书时,我可以选择该证书,如果有多个证书,我会让用户通过呼叫选择该证书C# C加载需要密码的证书,c#,security,x509certificate,x509certificate2,C#,Security,X509certificate,X509certificate2,我有下面的代码,当只有一个证书时,我可以选择该证书,如果有多个证书,我会让用户通过呼叫选择该证书 var certificates = X509Certificate2UI.SelectFromCollection(store.Certificates, "Digital Certificates", "Select a certificate from the following list:", X509SelectionFlag
var certificates = X509Certificate2UI.SelectFromCollection(store.Certificates,
"Digital Certificates", "Select a certificate from the following list:",
X509SelectionFlag.SingleSelection);
我注意到的一件事是,当只有一个证书时,我不需要输入预期的密码,因为我使用该证书从我的计算机登录;但有多个证书,我必须键入密码,我不想,因为我已经键入了密码,当我登录到windows系统;任何帮助/想法都将不胜感激
完整代码段:
X509Certificate2 certificate = null;
var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
try
{
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
if (store.Certificates.Count == 1) {
//Return the certificate present.
certificate = store.Certificates[0];
}
else if (store.Certificates.Count > 0)
{
// Request the user to select a certificate
var certificates = X509Certificate2UI.SelectFromCollection(store.Certificates,
"Digital Certificates", "Select a certificate from the following list:",
X509SelectionFlag.SingleSelection);
// Check if one has been returned
if (certificates.Count == 1) {
certificate = certificates[0];
}
else {
throw new ArgumentException("Please select a certificate to publish PnL to Flash");
}
}
else {
throw new ArgumentException("There is no certificate available to publish PnL to flash, please contact support.");
}
}
finally {
store.Close();
}
return certificate;
嗯,这取决于证书的状态