title: How do I authenticate to an API with a clientID and secret in a .NET Core 6 console application?
body: <p>I am still very new to programming in .net. I am trying to write a simple console app to test some endpoints in our scheduling software. They provide the following on how to authenticate to receive a JWT token for the next step in authentication.</p>
  <pre><code>POST /platform/api/v1/clientauthentication HTTP/1.1
  Content-Type: application/json
  
  { &quot;clientID&quot;: &quot;&quot;, &quot;secret&quot;: &quot;&quot; }
  </code></pre>
  <p>This returns a JWT which in turn requires me to authenticate as a user to access some of the endpoints</p>
  <pre><code>POST /platform/api/v1/authentication/token HTTP/1.1
  Content-Type: application/json
  
  X-applicationname-api-token: (client token goes in this header)
  
  { &quot;username&quot;: &quot;test&quot;, &quot;password&quot;: &quot;test&quot; }
  </code></pre>
  <p>I'm trying to use System.Net.Http, but once I create the client and set the base address I am lost on how to set the headers. I've searched and can't find anything other than heavily convoluted examples. I come from Python and it is so much easier to do. I just can't get my head around this in C#.</p>
  <p>Thank you for any responses.</p>
  <p><strong>Edit</strong>: I figured out a way to do this, but it just seems...wrong:</p>
  <pre><code>using (var client = new HttpClient())
  {
      client.BaseAddress = new Uri(API_URL);
      client.DefaultRequestHeaders.Accept.Clear();
      client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(&quot;application/json&quot;));
  
      var headers = new Dictionary&lt;string, string&gt;();
      headers.Add(&quot;clientID&quot;, CLIENT_ID);
      headers.Add(&quot;secret&quot;, API_SECRET);
  
      var response = client.PostAsJsonAsync(&quot;clientauthentication&quot;, headers).Result.Content.ReadAsStringAsync().Result;
  
      var json = JObject.Parse(response);
  }
  </code></pre>
  <p>I'm sure there is a better way. The more code I try to write, the more I learn I hardly know anything. A bit of a bummer.</p>