JWT Token Expiration and Authorization

Ronald Rex 1,671 Reputation points
2022-05-11T17:15:23.547+00:00

I am working with a JWT in .Net Core 6.0 Web Api. I am struggling with JWT expiration time...not sure if it is being set correctly with the UTC being different from my loacl time. But I also am having an issue with not being able to run an API Method after I Annotate the Method with [Authorize], even though I generate a token and send it with the request in postman. Really odd behavior with the JWT and expiration. Thanks for any help !!!

Developer technologies | .NET | Entity Framework Core
Microsoft Security | Microsoft Identity Manager
Developer technologies | ASP.NET Core | Other

Answer accepted by question author
AgaveJoe 31,706 Reputation points
2022-05-12T11:13:11.483+00:00

I noticed the configuration sets issuer and audience validation but the login action does not set these values.

                     x.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
                     {
                         ValidateIssuerSigningKey = true,
                         IssuerSigningKey = new SymmetricSecurityKey(key),
                         ValidateIssuer = true,
                         ValidateAudience = true,
                         ValidateLifetime = true,
                         ClockSkew = TimeSpan.Zero
                     };

Either set the configuration values to false or add the issuer and audience claims to the token.

public string Authenticate(string username, string password)
{
    var user = _users.SingleOrDefault(x => x.Username == username && x.Password == password);

    // return null if user not found
    if (user == null)
        return null;

    // authentication successful so generate jwt token
    var tokenHandler = new JwtSecurityTokenHandler();
    var key = Encoding.ASCII.GetBytes(Configuration["JwtConfig:secret"]);
    var tokenDescriptor = new SecurityTokenDescriptor
    {
        Subject = new ClaimsIdentity(new Claim[]
        {
            new Claim(ClaimTypes.Name, user.Id.ToString()),
        }),
        IssuedAt = DateTime.UtcNow,
        Expires = DateTime.UtcNow.AddMinutes(10),
        SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature),
        Issuer = "Issuer",
        Audience = "Audience"
    };
    var token = tokenHandler.CreateToken(tokenDescriptor);
    return tokenHandler.WriteToken(token);
}

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Bruce (SqlWork.com) 85,786 Reputation points
    2022-05-11T21:00:00.793+00:00

    typically an access token is good for 60 minutes. does the token have the scope to call the method? it a valid access token or an id token?

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.