title: MVC accessing external Web API using login credentials
body: <p>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:</p>
  
  <p>"The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel."</p>
  
  <p>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.</p>
  
  <pre><code> private static async Task&lt;string&gt; 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&lt;string,string&gt;("username",userName),
                  new KeyValuePair&lt;string,string&gt;("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;
          }
      }
  </code></pre>
  
  <p>Any help would be greatly appreciated.</p>
  
  <p>Thanks</p>