コード例 #1
0
        /// <summary>
        /// Exibe a lista de certificados instalados no PC e devolve o certificado selecionado
        /// </summary>
        /// <returns></returns>
        public static X509Certificate2 ListareObterDoRepositorio()
        {
            var store       = CertificadoDigital.ObterX509Store(OpenFlags.OpenExistingOnly | OpenFlags.ReadOnly);
            var collection  = store.Certificates;
            var fcollection = collection.Find(X509FindType.FindByTimeValid, DateTime.Now, true);
            var scollection = X509Certificate2UI.SelectFromCollection(fcollection, "Certificados válidos:", "Selecione o certificado que deseja usar",
                                                                      X509SelectionFlag.SingleSelection);

            if (scollection.Count == 0)
            {
                throw new Exception("Nenhum certificado foi selecionado!");
            }

            store.Close();
            return(scollection[0]);
        }
コード例 #2
0
        public List <Certificado> List()
        {
            try
            {
                using (var store = CertificadoDigital.ObterX509Store(OpenFlags.OpenExistingOnly | OpenFlags.ReadOnly))
                {
                    var result = new List <Certificado>();

                    foreach (var certificate in store.Certificates)
                    {
                        var friendlyName = certificate.FriendlyName;
                        if (string.IsNullOrEmpty(certificate.FriendlyName))
                        {
                            friendlyName = certificate.Subject;
                            if (friendlyName.Contains(", OU="))
                            {
                                friendlyName = friendlyName.Substring(0, friendlyName.IndexOf(", OU=")).Replace("CN=", string.Empty);
                            }
                        }
                        var emissor = certificate.IssuerName.Name;
                        if (emissor.Contains(", OU="))
                        {
                            emissor = emissor.Substring(0, emissor.IndexOf(", OU=")).Replace("CN=", string.Empty);
                        }

                        result.Add(new Certificado
                        {
                            Documento       = friendlyName?.Split(':').LastOrDefault(),
                            Responsavel     = friendlyName?.Split(':').FirstOrDefault(),
                            Emissor         = emissor,
                            ValidadeInicial = certificate.NotBefore,
                            ValidadeFinal   = certificate.NotAfter,
                            SerialNumber    = certificate.SerialNumber,
                            HasPrivateKey   = certificate.HasPrivateKey
                        });
                    }

                    store.Close();

                    return(result);
                }
            }
            catch (Exception)
            {
                return(new List <Certificado>());
            }
        }