title: c# asp.net core Bearer error="invalid_token"
body: <p>Can someone please help me to solve this problem? I'm testing the API using Postman</p>
  <p>I'm following a tutorial about asp.net core.</p>
  <p>And I'm on its Authentication part now.</p>
  <p>I don't really understand whats the reason for the error.</p>
  <p>In the tutorial, it has a login and it returns token.</p>
  <p>This is the code for login. Which is working. I know this is working because it returns a token. I also tried using an invalid login. and it returns <code>401 Unauthorized</code> But when I use the correct login credentials which are found in the database. It returns token</p>
  <pre><code>[HttpPost(&quot;login&quot;)]
  public async Task&lt;IActionResult&gt; Login(UserForLoginDto userForLoginDto)
      {
          var userFromRepo = await _repo.Login(userForLoginDto.Username.ToLower(), userForLoginDto.Password);
  
          if (userFromRepo == null)
              return Unauthorized();
  
          var claims = new[]
          {
              new Claim(ClaimTypes.NameIdentifier, userFromRepo.Id.ToString()),
              new Claim(ClaimTypes.Name, userFromRepo.Username)
          };
  
          var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config.GetSection(&quot;AppSettings:Token&quot;).Value));
  
          var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256Signature);
  
          var tokenDescriptor = new SecurityTokenDescriptor
          {
              Subject = new ClaimsIdentity(claims),
              Expires = DateTime.Now.AddDays(1),
              SigningCredentials = creds
          };
  
          var tokenHandler = new JwtSecurityTokenHandler();
  
          var token = tokenHandler.CreateToken(tokenDescriptor);
  
          return Ok(new {
              token = tokenHandler.WriteToken(token)
          });
  }
  </code></pre>
  <p>Then the next part of the tutorial is to limit the access. The user should be logged in first in order to view the content.</p>
  <p>Below is the code</p>
  <pre><code>services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
              .AddJwtBearer(options =&gt;{
                  options.TokenValidationParameters = new TokenValidationParameters{
                      ValidateIssuerSigningKey = true,
                      IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(Configuration.GetSection(&quot;AppSettings:Token&quot;).Value)),
                      ValidateIssuer = false
                  };
              });
  </code></pre>
  <p>Then enabled</p>
  <pre><code>app.UseAuthentication();
  </code></pre>
  <p>I also enabled the <code>[Authorize]</code> in the Values Controller</p>
  <pre><code>[Authorize]
  [Route(&quot;api/[controller]&quot;)]
  [ApiController]
  public class ValuesController : ControllerBase
  </code></pre>
  <p>This is the screenshot of postman</p>
  <p><a href="https://i.stack.imgur.com/Dg0Yr.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/Dg0Yr.jpg" alt="enter image description here" /></a></p>
  <p>I followed the tutorial. I paste the token I received from login. But it gives me the error</p>
  <pre><code>WWW-Authenticate →Bearer error=&quot;invalid_token&quot;, error_description=&quot;The audience is invalid&quot;
  </code></pre>
  <p>Why does the error give me <code>invalid token</code> if the token is from the login? How do I fix this? I've been searching for a while but I can't solve this my self. Thank you.</p>