我有下面的代码,当只有一个证书时,我可以选择证书,如果有超过1个证书,我会要求用户通过调用
来选择证书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;
答案 0 :(得分:0)
嗯,这取决于证书的状态。