0

In need of some help accessing an external Web API passing along credentials in order to access the methods available. I have included the code below that i use in order to attempt to access the Web API. However, i receive the following error every time i attempt to access it:

"The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel."

What am i missing or what am i doing wrong? I have been circling around this for a couple days and have tried a couple different techniques but continue to get the same error. Here is one technique that i used.

 private static async Task<string> GetAPIToken(string userName, string password, string apiBaseUri)
    {
        try
        {
            using (var client = new HttpClient())
            {
                //setup client
                client.BaseAddress = new Uri(apiBaseUri);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                //setup login data
                var formContent = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair<string,string>("username",userName),
                new KeyValuePair<string,string>("password",password),
            });

                //send request
                HttpResponseMessage responseMessage = await client.PostAsync("Token", formContent);

                //get access token from response body
                var responseJson = await responseMessage.Content.ReadAsStringAsync();
                var jobject = JObject.Parse(responseJson);
                return jobject.GetValue("access_token").ToString();
            }
        }
        catch (Exception ex)
        {
            return null;
        }
    }

Any help would be greatly appreciated.

Thanks

Alex
  • 67
  • 1
  • 10

1 Answers1

0

There is a little bit of a difference when using HTTPS vs HTTP. This question should give you the information you need to fix your problem.

Make Https call using HttpClient

Derek Hackett
  • 190
  • 10