【发布时间】:2015-04-14 02:37:11
【问题描述】:
我有下面的代码,当只有一个证书时,我可以选择证书,如果有多个证书,我会通过调用要求用户选择证书
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;
【问题讨论】:
标签: c# security x509certificate x509certificate2