Exemple #1
0
 public AccountManagerAccessTokenAccessor(IOptions <SFCCEnvironment> envOptions, IHttpClientFactory clientFactory,
                                          ILogger <AccountManagerAccessTokenAccessor> logger)
 {
     _environment = envOptions.Value;
     _environment.IsValidEnvironment(throwOnInvalid: true);
     _environment.HasClientCredentials(throwOnInvalid: true);
     _clientFactory = clientFactory;
     _logger        = logger;
 }
Exemple #2
0
        private HttpClient GetClient()
        {
            HttpClient client;

            if (_env.HasClientCredentials())
            {
                // use client provided by account manager integration
                client = _clientFactory.CreateClient("ClientCredentialsGrant");
            }
            else
            {
                // Fallback behavior if no client credentials are available for the environment
                client = _clientFactory.CreateClient("BasicAuth");
                var byteArray = Encoding.ASCII.GetBytes($"{_env.Username}:{_env.Password}");
                client.DefaultRequestHeaders.Authorization =
                    new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
            }

            client.BaseAddress = new Uri($"https://{_env.Server}");
            return(client);
        }