private void FillCertificates(NamedStore store, string subjectName) { cmbCertificates.Items.Clear(); IEnumerable <X509Certificate2> certificates = null; try { certificates = CertificateHelper.LoadUserCertificates(store, subjectName); } catch (Exception ex) { cmbCertificates.Items.Add(new ComboBoxItem(null, ex.Message)); } if (certificates != null) { foreach (var cert in certificates) { cmbCertificates.Items.Add(new ComboBoxItem(cert, cert.SubjectName.Name)); } if (cmbCertificates.Items.Count > 0) { cmbCertificates.SelectedIndex = 0; } } }
/// <summary> /// Init constructor. /// </summary> public SignData(X509Certificate2 certificate, NamedStore certificateStore, string certificatePath, string certificatePassword, string timestampServer, NamedHashAlgorithm hashAlgorithm) { if (certificate == null && string.IsNullOrEmpty(certificatePath)) { throw new ArgumentException("certificate"); } if (certificate != null && certificateStore == null) { throw new ArgumentNullException("certificateStore"); } _certificate = certificate; _certificateStore = certificateStore; _certificatePath = certificatePath; _certificatePassword = certificatePassword; _timestampServer = timestampServer; _hashAlgorithm = hashAlgorithm; }
/// <summary> /// Loads all certificates that belong to current user. /// </summary> public static IEnumerable <X509Certificate2> LoadUserCertificates(NamedStore store, string subjectName) { var certStore = store == null ? new X509Store(StoreName.My, StoreLocation.CurrentUser) : new X509Store(store.Name, store.Location); try { certStore.Open(OpenFlags.ReadOnly); var result = new List <X509Certificate2>(); var certificates = string.IsNullOrEmpty(subjectName) ? certStore.Certificates : certStore.Certificates.Find(X509FindType.FindBySubjectName, subjectName, true); foreach (var cert in certificates) { result.Add(cert); } return(result); } finally { certStore.Close(); } }