예제 #1
0
        public static async Task <HttpClient> GetHttpClientWithToken(string urlService, string username = null, string password = null, string token = null)
        {
            //Create and Load Http Client
            HttpClient client = GetHttpClient(urlService: urlService);

            //Get Token
            if (string.IsNullOrEmpty(token) && !string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
            {
                AuthenticationRequest authenticationRequest = new AuthenticationRequest(client);
                token = await authenticationRequest.GetToken(username, password);
            }
            //Load Token to Http header
            if (!string.IsNullOrEmpty(token))
            {
                client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
            }
            return(client);
        }