title: Login via HttpClient in Web API Exception await client.PostAsync
body: <p>I have created an ASP.net project WebAPI and an MVC Project.
  When i try to login via HttpClient with the mvc project to the web api it throws an exception.</p>
  
  <p>MVC code,</p>
  
  <p>Login in AccountController:</p>
  
  <pre><code>  public async Task&lt;ActionResult&gt; Login(LoginViewModel model, string returnUrl)
      {
          if (!ModelState.IsValid)
          {
              return View(model);
          }
          try
          {
              var client = WebApiHttpClient.GetClient();
              string username = model.Email;
              string password = model.Password;
              HttpContent content = new StringContent("grant_type=password&amp;username=" + username + "&amp;password=" + password,System.Text.Encoding.UTF8,
              "application/x-www-form-urlencoded");
              var response = await client.PostAsync("/Token", content);
              if (response.IsSuccessStatusCode)
              {
                  //string contentResponse = await response.Content.ReadAsStringAsync();
                  //return Content(contentResponse);
                  TokenResponse tokenResponse =
                  await response.Content.ReadAsAsync&lt;TokenResponse&gt;();
                  WebApiHttpClient.storeToken(tokenResponse);
                  // return Content(tokenResponse.AccessToken);
                  return RedirectToAction("Index", "Home");
  
              }
              else
              {
                  return Content("Error in: " + response.StatusCode);
              }
          }
          catch
          {
              return Content("Error catch .");
          }
      }
  </code></pre>
  
  <p>WebapiHtppClient class: used to connect to the API</p>
  
  <pre><code>  public class WebApiHttpClient
  {
      public const string WebApiBaseAddress = "https://localhost:44379/";
      public static HttpClient GetClient()
      {
          HttpClient client = new HttpClient();
          client.BaseAddress = new Uri(WebApiBaseAddress);
          client.DefaultRequestHeaders.Accept.Clear();
          client.DefaultRequestHeaders.Accept.Add(new
          MediaTypeWithQualityHeaderValue("application/json"));
          var session = HttpContext.Current.Session;
          if (session["token"] != null)
          {
              TokenResponse tokenResponse = getToken();
              client.DefaultRequestHeaders.Authorization =
              new AuthenticationHeaderValue("bearer", tokenResponse.AccessToken);
          }
  
          return client;
      }
  
      public static void storeToken(TokenResponse token)
      {
          var session = HttpContext.Current.Session;
          session["token"] = token;
      }
      public static TokenResponse getToken()
      {
          var session = HttpContext.Current.Session;
          return (TokenResponse)session["token"];
      }
  
  }
  
  public class TokenResponse
  {
      [JsonProperty("access_token")]
      public string AccessToken { get; set; }
      [JsonProperty("token_type")]
      public string TokenType { get; set; }
      [JsonProperty("expires_in")]
      public int ExpiresIn { get; set; }
      [JsonProperty("userName")]
      public string Username { get; set; }
      [JsonProperty(".issued")]
      public string IssuedAt { get; set; }
      [JsonProperty(".expires")]
      public string ExpiresAt { get; set; }
  } 
  </code></pre>
  
  <p>i already tried a lot of anwsers from here but didnt find results.
  One was in the await client.PostAsync("/Token", content) , just put the "Token"</p>
  
  <p>With the Postman from chrome i can do the login and see the stuff in the Web Api
  with this</p>
  
  <p><a href="https://i.stack.imgur.com/mlynr.png" rel="nofollow noreferrer">Postman</a> </p>
  
  <p>Error catch:An error occurred while sending the request. mscorlib at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at WebApplication1.Controllers.AccountController.d__11.MoveNext() in C:\wamp\www\arqsi\it1\iteracao3\iteracao3\WebApplication1\Controllers\AccountController.cs:line 88</p>