Exemple #1
0
 private TokenCredential CreateTokenCredential(AuditClientMicrosoftIdentityClientOptions optionsValue)
 {
     return(new ChainedTokenCredential(
                new ManagedIdentityCredential(optionsValue.ClientId),
                new ClientSecretCredential(optionsValue.TenantId, optionsValue.ClientId, optionsValue.ClientSecret)
                ));
 }
        public static IServiceCollection AddAuditClientAzureIdentity(this IServiceCollection services, IConfigurationSection section)
        {
            Guard.NotNull(services, nameof(services));

            var auditClientAzureIdentityOptions = new AuditClientMicrosoftIdentityClientOptions();

            section.Bind(auditClientAzureIdentityOptions);

            return(services.AddAuditClientAzureIdentity(auditClientAzureIdentityOptions));
        }
        public static IServiceCollection AddAuditClientAzureIdentity(this IServiceCollection services, Action <AuditClientMicrosoftIdentityClientOptions> configureAction)
        {
            Guard.NotNull(services, nameof(services));
            Guard.NotNull(configureAction, nameof(configureAction));

            var options = new AuditClientMicrosoftIdentityClientOptions();

            configureAction(options);

            return(services.AddAuditClientAzureIdentity(options));
        }
        public AuditClientAzureIdentity(
            ILogger <AuditClientAzureIdentity> logger,
            IOptions <AuditClientMicrosoftIdentityClientOptions> options,
//            IHttpClientFactory factory,
            HttpClient client)
        {
            _logger  = logger;
            _options = options.Value;
            //          _clientFactory = factory;
            _client = client;
        }
        private static IServiceCollection AddAuditClientAzureIdentity(this IServiceCollection services, AuditClientMicrosoftIdentityClientOptions options)
        {
            if (string.IsNullOrEmpty(options.HttpClientName))
            {
                options.HttpClientName = AuditClientAzureIdentityConstants.Name;
            }

            services
            //.AddSingleton(ConfidentialClientApplicationBuilder
            //    .Create(options.ClientId)
            //    .WithClientSecret(options.ClientSecret)
            //    .WithAuthority($"https://login.microsoftonline.com/{options.TenantId}/")
            //    .Build()
            //)

            .AddTransient <AuthenticationHttpMessageHandler>()
            .AddHttpClient <IAuditClientAzureIdentity, AuditClientAzureIdentity>(options.HttpClientName, httpClient =>
            {
                httpClient.BaseAddress = options.BaseAddress;
            })
            .AddHttpMessageHandler <AuthenticationHttpMessageHandler>()
            .AddPolicyHandler((serviceProvider, request) => AuditClientPolicies.RetryPolicy(serviceProvider));
            //.AddPolicyHandler((serviceProvider, request) =>
            //HttpPolicyExtensions.HandleTransientHttpError()
            //    .WaitAndRetryAsync(3,
            //        sleepDurationProvider: retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
            //        onRetry: (outcome, timespan, retryAttempt, context) =>
            //        {
            //            serviceProvider.GetService<ILogger<AuditClientAzureIdentity>>()
            //                .LogWarning("Delaying for {delay}ms, then making retry {retry}.", timespan.TotalMilliseconds, retryAttempt);
            //        }
            //    ));

            services.AddHttpClient("google", httpClient =>
            {
                httpClient.BaseAddress = new Uri("https://www.google.com");
            });

            services.AddSingleton(Options.Options.Create(options));

            services.AddSingleton <IAccessTokenService, AccessTokenService>();
            //services.AddSingleton<IAuditClientAzureIdentity, AuditClientAzureIdentity>();

            services.AddMemoryCache();

            return(services);
        }