public async Task SaveAccountAsync(AccountModel account, CancellationToken cancellationToken)
        {
            var secretName = GetSecretName();

            _logger.LogTrace("Saving account information to Azure Key Vault as {secretName}", secretName);
            var secretValue = JsonSerializer.Serialize(account);

            try
            {
                var secretClient = _secretClientFactory.Create();

                await secretClient.SetSecretAsync(secretName, secretValue, cancellationToken);

                _logger.LogInformation("Saved account information to Azure Key Vault as {secretName}", secretName);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Failed to save account information to Azure Key Vault as {secretName}",
                                 secretName);
                throw;
            }
        }
Exemplo n.º 2
0
        private async Task <X509Certificate2?> GetCertificateWithPrivateKeyAsync(string domainName, CancellationToken token)
        {
            _logger.LogDebug("Searching for certificate in KeyVault for {domainName}", domainName);

            try
            {
                var normalizedName = NormalizeHostName(domainName);
                var secretClient   = _secretClientFactory.Create();

                var certificate = await secretClient.GetSecretAsync(normalizedName, null, token);

                var cert = new X509Certificate2(Convert.FromBase64String(certificate.Value.Value));

                _logger.LogInformation(
                    "Found certificate for {domainName} from Azure Key Vault with thumbprint {thumbprint}",
                    domainName, cert.Thumbprint);

                return(cert);
            }
            catch (RequestFailedException ex) when(ex.Status == 404)
            {
                _logger.LogInformation("Could not find certificate for {domainName} in Azure KeyVault", domainName);
            }
            catch (CredentialUnavailableException ex)
            {
                _logger.LogError(ex, "Could not retrieve credentials for Azure Key Vault");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex,
                                 "Unexpected error attempting to retrieve certificate for {domainName} from Azure KeyVault. Verify settings and try again.",
                                 domainName);
            }

            return(null);
        }