public async Task <ViewModel> Handle(Query request, CancellationToken cancellationToken) { var viewModel = new ViewModel(); viewModel.ValidationResult = await queryValidator.ValidateAsync(request, cancellationToken); if (!viewModel.ValidationResult.IsValid) { return(viewModel); } var certificate = await privateCertRepository.GetCertificateAsync(request.CertificateId); var passphrase = await privateCertRepository.GetPassphraseAsync(); var passphraseDecrypted = StringCipher.Decrypt(passphrase, request.MasterKeyDecrypted); var x509 = new X509Certificate2(); x509.Import(certificate.PfxData, passphraseDecrypted, X509KeyStorageFlags.Exportable); viewModel.CertificateData = x509.Export(certificate.CertificateType == CertificateTypeEnum.Root ? X509ContentType.Cert : X509ContentType.Pfx, string.Empty); viewModel.Extension = (certificate.CertificateType == CertificateTypeEnum.Root ? ".cer" : ".pfx"); viewModel.ExtensionFilter = (certificate.CertificateType == CertificateTypeEnum.Root ? "Security Certificate (.cer)|*.cer" : "PKCS #12 Certificate (.pfx)|*.pfx"); viewModel.FileNameSuggestion = certificate.Name.Replace("*", "_") + viewModel.Extension; return(viewModel); }
private void CertificateExists(Query query, CustomContext customContext) { if (privateCertRepository.GetCertificateAsync(query.CertificateId) == null) { customContext.AddFailure($"Certificate with Id {query.CertificateId} does not exists."); } }
public async Task <ValidationResult> Handle(Command command, CancellationToken cancellationToken) { unitOfWork.BeginTransaction(); var result = await commandValidator.ValidateAsync(command, cancellationToken); if (!result.IsValid) { return(result); } var passphrase = await privateCertRepository.GetPassphraseAsync(); var passphraseDecrypted = StringCipher.Decrypt(passphrase, command.MasterKeyDecrypted); var parentCertificate = await privateCertRepository.GetCertificateAsync(command.SelectedAuthorityCertificateId); var certificate = Certificate.CreateClientCertificate(command, parentCertificate, passphraseDecrypted); await privateCertRepository.AddCertificateAsync(certificate); unitOfWork.SaveChanges(); unitOfWork.CommitTransaction(); return(result); }