public static void AddAuthenticatedHttpClient(this IServiceCollection services, Action <AuthenticatedHttpClientOptions> setupAction = null)
        {
            var options = new AuthenticatedHttpClientOptions();

            setupAction?.Invoke(options);
            services.AddSingleton(options);

            services.TryAddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            services.TryAddSingleton <AuthenticatedHttpMessageHandler>();

            services.AddTransient <Func <AuthenticatedHttpMessageHandler> >(
                sp => () => sp.GetService <AuthenticatedHttpMessageHandler>());

            services.AddTransient <IAuthenticatedHttpClientFactory, AuthenticatedHttpClientFactory>();
        }
Ejemplo n.º 2
0
        public AuthenticatedHttpMessageHandler(AuthenticatedHttpClientOptions options,
                                               IHttpContextAccessor httpContextAccessor,
                                               ILogger <AuthenticatedHttpMessageHandler> logger)
        {
            _options             = options ?? throw new ArgumentNullException(nameof(options));
            _httpContextAccessor = httpContextAccessor;
            _logger = logger;

            _tokenClient = new TokenClient(_options.TokenServiceAddress,
                                           _options.ClientId,
                                           _options.TokenServiceMessageHandler ?? new HttpClientHandler());
            _tokenClient.AuthenticationStyle = AuthenticationStyle.BasicAuthentication;

            _semaphoreSlim   = new SemaphoreSlim(1, 1);
            _resetTokenAfter = null;

            var handler = options.InnerMessageHandler ?? new HttpClientHandler();

            _httpMessageInvoker = new HttpMessageInvoker(handler, true);
        }