// This method works specifically for single tenant application.
        private static void RegisterAuthenticationServices(
            IServiceCollection services,
            IConfiguration configuration,
            AuthenticationOptions authenticationOptions)
        {
            AuthenticationServiceCollectionExtensions.ValidateAuthenticationOptions(authenticationOptions);

            services.AddProtectedWebApi(configuration)
            .AddProtectedWebApiCallsProtectedWebApi(configuration)
            .AddInMemoryTokenCaches();
            services.Configure <JwtBearerOptions>(JwtBearerDefaults.AuthenticationScheme, options =>
            {
                var azureADOptions = new AzureADOptions
                {
                    Instance = authenticationOptions.AzureAdInstance,
                    TenantId = authenticationOptions.AzureAdTenantId,
                    ClientId = authenticationOptions.AzureAdClientId,
                };
                options.Authority = $"{azureADOptions.Instance}{azureADOptions.TenantId}/v2.0";
                options.SaveToken = true;
                options.TokenValidationParameters.ValidAudiences    = AuthenticationServiceCollectionExtensions.GetValidAudiences(authenticationOptions);
                options.TokenValidationParameters.AudienceValidator = AuthenticationServiceCollectionExtensions.AudienceValidator;
                options.TokenValidationParameters.ValidIssuers      = AuthenticationServiceCollectionExtensions.GetValidIssuers(authenticationOptions);
            });
        }
        // This method works specifically for single tenant application.
        private static void RegisterAuthenticationServices(
            IServiceCollection services,
            IConfiguration configuration,
            AuthenticationOptions authenticationOptions)
        {
            AuthenticationServiceCollectionExtensions.ValidateAuthenticationOptions(authenticationOptions);
            var azureADOptions = new AzureADOptions
            {
                Instance = authenticationOptions.AzureAdInstance,
                TenantId = authenticationOptions.AzureAdTenantId,
                ClientId = authenticationOptions.AzureAdClientId,
            };
            var useCertificate = configuration.GetValue <bool>("UseCertificate");

            if (useCertificate)
            {
                RegisterAuthenticationServicesWithCertificate(services, configuration, authenticationOptions, azureADOptions);
            }
            else
            {
                RegisterAuthenticationServicesWithSecret(services, configuration, authenticationOptions, azureADOptions);
            }
        }