示例#1
0
        /// <summary>
        /// Найти сертификат получателя в хранилище AddressBook
        /// </summary>
        /// <param name="subjectName"></param>
        /// <returns></returns>
        public Task <X509Certificate2> FindAddressBookCertificateBySubjectName(string subjectName)
        {
            X509Store store = new X509Store(StoreName.AddressBook, CurrentInfo.IsMachine() ? StoreLocation.LocalMachine : StoreLocation.CurrentUser);

            store.Open(OpenFlags.ReadOnly);

            X509Certificate2 recipientCertificate = null;

            // Перебираем сертификаты и ищем по имени
            foreach (X509Certificate2 certificate in store.Certificates)
            {
                if (certificate.SubjectName.Name != null)
                {
                    if (certificate.SubjectName.Name.Contains(subjectName))
                    {
                        recipientCertificate = certificate;
                        break;
                    }
                }
            }

            store.Close();

            if (recipientCertificate == null)
            {
                throw new Exception($"Сертификат по наименованию {subjectName} в хранилище не найден");
            }

            return(Task.FromResult(recipientCertificate));
        }