title: ASP.NET Core application http client request to a web api
body: <p>I have a running Web API and I try to get a bearer token back from it. Starting the request from Postman is working and I get the token back. Once doing from my application I always get an http 400 Bad Request error.</p>
  <p>What am I missing here?</p>
  <pre><code>public async Task&lt;string&gt; GetToken(string userName, string passWord)
  {
      var request = new HttpRequestMessage(HttpMethod.Post, &quot;api/auth/login&quot;);
  
      request.Headers.Authorization = new AuthenticationHeaderValue(
                  &quot;Basic&quot;, Convert.ToBase64String(Encoding.UTF8.GetBytes($&quot;{ userName}:{ passWord}&quot;)));
      request.Headers.Host = &quot;api.my-host.com&quot;;
      request.Headers.Accept.Add(
                  new MediaTypeWithQualityHeaderValue(&quot;application/json&quot;));
              
      var response = await _httpClient.SendAsync(request);
  
      response.EnsureSuccessStatusCode();
  
      using var responseStream = await response.Content.ReadAsStreamAsync();
  
      var authResult = await JsonSerializer.DeserializeAsync&lt;AuthResult&gt;(responseStream);
  
      return authResult == null ? &quot;&quot; : authResult.Access_Token;
  }
  </code></pre>
  <p>As requested here's a screenshot of the Postman result:</p>
  <p><img src="https://i.stack.imgur.com/9KEll.png" alt="PostmanResult" /></p>
  <p>I created a HttpGet request and added the bearer token from Postman in the code and I receive data. Just the token request seems to have a problem.</p>
  <p>And my controller:</p>
  <pre><code>namespace AmsAPI.Controller
  {
  
      [Produces(&quot;application/json&quot;)]
      [Route(&quot;api/auth&quot;)]
      [ApiController]
      [Authorize]
      public class AuthenticationController : ControllerBase
      {
          private readonly IAuthenticationManager _authenticationManager;
  
          public AuthenticationController(IAuthenticationManager authenticationManager)
          {
              _authenticationManager = authenticationManager;
          }
  
          [HttpPost(&quot;login&quot;), AllowAnonymous]
          [ProducesResponseType(StatusCodes.Status200OK)]
          [ProducesDefaultResponseType]
          public async Task&lt;ActionResult&gt; Login([FromHeader] byte[] basic)
          {
              if (!ModelState.IsValid) return BadRequest();
  
              string Basic = Encoding.UTF8.GetString(basic);
  
              var splitBasic = Basic.Split(':');
  
              AuthCredentials credentials = new()
              {
                  UserName = splitBasic[0],
                  Password = splitBasic[1]
              };
  
              return await _authenticationManager.SignInCheck(credentials) ?
                  Ok(new
                  {
                      message = string.Format(&quot;User {0} successfully logged in.&quot;, credentials.UserName),
                      access_token = await _authenticationManager.CreateToken(),
                      token_type = &quot;bearer&quot;,
                      expires_in = &quot;3600&quot;
                  }) :
                  Unauthorized();
          }
  
          [HttpGet(&quot;user&quot;)]
          [ProducesResponseType(StatusCodes.Status200OK)]
          [ProducesDefaultResponseType]
          public async Task&lt;List&lt;User&gt;&gt; GetUser() =&gt; await _authenticationManager.GetUser();
      }
  }
  </code></pre>