public static ApiUrlSettings GetApiUrl(this IConfiguration configuration)
        {
            var apiUrlSetting = new ApiUrlSettings();

            configuration?.GetSection("ApiUrlSettings")?.Bind(apiUrlSetting);
            return(apiUrlSetting);
        }
예제 #2
0
        public HttpClientFactoryService(HttpClient httpClient, IMemoryCache memoryCache)
        {
            _cache = memoryCache;
            var environment        = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
            var appSettingJsonFile = environment == EnvironmentName.Development
                ? "appsettings.json"
                : "appsettings.production.json";
            var builder = new ConfigurationBuilder()
                          .SetBasePath(Directory.GetCurrentDirectory())
                          .AddJsonFile(appSettingJsonFile);
            var configuration = builder.Build();

            if (configuration != null)
            {
                _apiUrls        = configuration.GetApiUrl();
                _apiServiceInfo = configuration.GetApiServiceInfo();
            }

            Client = Task.Run(GetClient).Result;
            ServicePointManager.SecurityProtocol =
                SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

            #region Local Function.
            async Task <HttpClient> GetClient()
            {
                var client = new HttpClient();

                if (_cache.TryGetValue($"{CacheParamPublic.AccessToken}", out string access_token))
                {
                    client.SetBearerToken(access_token);
                    return(client);
                }
                else
                {
                    //if (string.IsNullOrEmpty(_apiUrls.Authority))
                    //    return null;

                    //var disco = await DiscoveryClient.GetAsync(_apiUrls.Authority);
                    //if (disco.IsError)
                    //    return null;
                    //if (string.IsNullOrEmpty(_apiServiceInfo.ClientId) || string.IsNullOrEmpty(_apiServiceInfo.ClientSecret) || string.IsNullOrEmpty(_apiServiceInfo.Scopes))
                    //    return null;

                    //var tokenClient = new TokenClient(disco.TokenEndpoint, _apiServiceInfo.ClientId, _apiServiceInfo.ClientSecret);
                    //var tokenResponse = await tokenClient.RequestClientCredentialsAsync(_apiServiceInfo.Scopes);
                    //if (tokenResponse.IsError)
                    //    return null;
                    //client.SetBearerToken(tokenResponse.AccessToken);
                    //_cache.Set(CacheParamPublic.AccessToken, tokenResponse.AccessToken, TimeSpan.FromDays(1));
                    return(client);
                }
            }

            #endregion
        }
예제 #3
0
        public HttpClientService()
        {
            var environment        = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
            var appSettingJsonFile = environment == EnvironmentName.Development
                ? "appsettings.json"
                : "appsettings.production.json";
            var builder = new ConfigurationBuilder()
                          .SetBasePath(Directory.GetCurrentDirectory())
                          .AddJsonFile(appSettingJsonFile);
            var configuration = builder.Build();

            if (configuration != null)
            {
                _apiUrls        = configuration.GetApiUrl();
                _apiServiceInfo = configuration.GetApiServiceInfo();
            }
            Client = Task.Run(GetClient).Result;

            ServicePointManager.SecurityProtocol =
                SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

            #region Local Function.
            async Task <HttpClient> GetClient()
            {
                var client = new HttpClient();
                DiscoveryDocumentResponse discoveryDocument = await client.GetDiscoveryDocumentAsync(_apiUrls.Authority);

                var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
                {
                    Address      = discoveryDocument.TokenEndpoint,
                    ClientId     = _apiServiceInfo.ClientId,
                    ClientSecret = _apiServiceInfo.ClientSecret,
                    GrantType    = GrantTypes.ClientCredentials,
                    Scope        = _apiServiceInfo.Scopes
                });

                client.SetBearerToken(tokenResponse.AccessToken);
                return(client);
            }

            #endregion
        }