/// <inheritdoc/>
        public async Task <string?> RequestTokenAsync()
        {
            this.logger?.LogInformation("Requesting token from identity provider.");

            if (this.confidentialClientApplication == null)
            {
                ValidationResult validationResult = this.aadAppConfiguration.ValidationResult();
                if (!validationResult.Success)
                {
                    throw new ArgumentException(validationResult.Message);
                }

                if (!string.IsNullOrEmpty(this.aadAppConfiguration.CertificateSubjectName))
                {
                    this.confidentialClientApplication = ConfidentialClientApplicationBuilder.Create(this.aadAppConfiguration.ClientId)
                                                         .WithCertificate(CertificateFinder.FindBySubjectName(this.aadAppConfiguration.CertificateSubjectName, DateTime.UtcNow))
                                                         .WithAuthority(AzureCloudInstance.AzurePublic, this.aadAppConfiguration.TenantId)
                                                         .Build();
                }
                else
                {
                    this.confidentialClientApplication = ConfidentialClientApplicationBuilder.Create(this.aadAppConfiguration.ClientId)
                                                         .WithClientSecret(this.aadAppConfiguration.ClientSecret)
                                                         .WithAuthority(AzureCloudInstance.AzurePublic, this.aadAppConfiguration.TenantId)
                                                         .Build();
                }
            }

            AuthenticationResult?result;

            try
            {
                result = await this.confidentialClientApplication.AcquireTokenForClient(this.aadAppConfiguration.Scopes)
                         .ExecuteAsync();

                this.logger?.LogInformation("Token requested successfully.");
                this.logger?.LogDebug($"Access token: {result.AccessToken}");
                this.logger?.LogDebug($"Expires on  : {result.ExpiresOn}");
                this.logger?.LogDebug($"Scopes      : {string.Join(";", result.Scopes)}");
            }
            catch (MsalException e)
            {
                this.logger?.LogError(e, "Requesting token failed");
                throw;
            }

            return(result.AccessToken);
        }
 public IConfidentialClientApplication CreateConfidentialClientApplication(string certificateSubjectName) =>
 ConfidentialClientApplicationBuilder
 .Create(ClientId)
 .WithCertificate(CertificateFinder.FindBySubjectName(certificateSubjectName, DateTime.UtcNow))
 .WithAuthority(AzureCloudInstance.AzurePublic, TenantId)
 .Build();