title: Angular 5 authenticate with web api
body: <p>i try to post my login credentials to my asp.net core web api. but every time i do this i get this message.</p>
  
  <p>OPTIONS XHR 
  <a href="http://localhost:64989/api/auth/login" rel="nofollow noreferrer">http://localhost:64989/api/auth/login</a> [HTTP/1.1 415 Unsupported Media Type 16ms]</p>
  
  <p>ERROR Object { headers: Object, status: 0, statusText: "Unknown Error", url: null, ok: false, name: "HttpErrorResponse", message: "Http failure response for (unknown …", error: error }</p>
  
  <p>my code looks like this on Angular side:</p>
  
  <pre><code>login( email:string, password:string ) :Observable&lt;boolean&gt;{
  
    return this.http.post('http://localhost:64989/api/auth/login', {email, password}, {headers: new HttpHeaders().set('Content-Type', 'application/json')})
     .map(data =&gt; {
       let userAuth = data;
       if(userAuth){
         localStorage.setItem('currentUser', JSON.stringify(userAuth));
         return true;
       }else{
         return false;
       } 
     });
  }
  </code></pre>
  
  <p>On server side i've got a web api which should check the login credentials. this works with postman but not with angular. The Web Api is made with Asp.net core 2.</p>
  
  <pre><code> [AllowAnonymous]
      [HttpPost("CreateToken")]
      [Route("login")]
      public async Task&lt;IActionResult&gt; Login([FromBody] LoginDTO model)
      {
          try
          {
              var user = await _userManager.FindByNameAsync(model.Email);
              if (user == null)
              {
                  return Unauthorized();
              }
  
              var signInResult = await _signInManager.CheckPasswordSignInAsync(user, model.Password, false);
  
              if (signInResult.Succeeded)
              {
                  var userClaims = await _userManager.GetClaimsAsync(user);
  
                  var claims = new[]
                  {
                      new Claim(JwtRegisteredClaimNames.Sub, user.UserName),
                      new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
                      new Claim(JwtRegisteredClaimNames.Email, user.Email)
                  }.Union(userClaims);
  
                  var symmetricSecurityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["JwtToken:Key"]));
                  var signingCredentials = new SigningCredentials(symmetricSecurityKey, SecurityAlgorithms.HmacSha256);
  
                  var jwtSecurityToken = new JwtSecurityToken(
                      issuer: _configuration["JwtToken:Issuer"],
                      audience: _configuration["JwtToken:Audience"],
                      claims: claims,
                      expires: DateTime.UtcNow.AddMinutes(60),
                      signingCredentials: signingCredentials
                      );
                  return Ok(new
                  {
                      token = new JwtSecurityTokenHandler().WriteToken(jwtSecurityToken),
                      expiration = jwtSecurityToken.ValidTo
                  });
              }
              return Unauthorized();
          }
          catch (Exception ex)
          {
              _logger.LogError($"error while creating token: {ex}");
              return StatusCode((int)HttpStatusCode.InternalServerError, "error while creating token");
          }
      }
  </code></pre>