Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

785
Visualizações
Jwt token in dotnet with no Exp and default props

I'am using .Net core 3.1 to make a indentity api that i register and make my user login , and with that i need to pass a JWT token for the frontend use.

I already generate my token with the claims i need, but i need the token to never expires (i know it's not a good practice but i'm just following orders), and also i need to remove token's default props such as nbf and iat.

I'm using the lib Microsoft.AspNetCore.Authentication.JwtBearer I did not found much in the documentation, so i don't even know if it's possible

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Have you tried setting ValidateLifetime to false in TokenValidationParameters? That would allow any expiration dates.

Another thing you can do is to set RequireExpirationTime to false, which means exp doesn't need to be present in the token.

So, if you want to configure it to allow tokens not to contain an expiration time, but still validate the expiration time if the exp property is present, you can do this:

.AddJwtBearer(options =>
{
    options.TokenValidationParameters = new TokenValidationParameters
    {
        ValidateLifetime = true,
        RequireExpirationTime = false,
    };
});

Also, if you want, you can set a custom lifetime validator at the same place:

.AddJwtBearer(options =>
{
    options.TokenValidationParameters = new TokenValidationParameters
    {
        // ... Other settings

        LifetimeValidator = (notBefore, expiration, token, parameters) =>
        {
            // Decide if expiration is valid. Don't forget about clock skew.
        }
    };
});

Although it's unclear if you have control over the token issuer, or the token consumer, or both. All the above solutions assume you have control over the token consumer.

If you have control only over the issuer, it's a bit more difficult. What comes to my mind is setting an exp date that is extremely far into the future. And for invalidating the tokens if it's later required, one thing you could do I suppose is to change the encryption key that the issuer and consumer uses to create/validate the signature.

Removing exp, iat and nbf at the issuer side

As for removing the properties from the token, you can just leave them our from the token generation. Assuming you're doing manual token generation, you'd want to configure it e.g. like this:

var tokenOptions = new JwtSecurityToken(
    issuer: jwtIssuer,
    audience: jwtAudience,
    claims: new List<Claim>() {
        new Claim(JwtRegisteredClaimNames.Sub, userName),
    },
    // 'expires' not set
    signingCredentials: new SigningCredentials(new SymmetricSecurityKey(Convert.FromBase64String(someKey)), SecurityAlgorithms.HmacSha256)
);

Generating it this way it won't contain exp, iat or nbf (confirmed locally).

Of course if you happen to generate it via e.g. Identity Server, it's a different story, but then you forgot to mention that (you mentioned Identity, which is a membership system that doesn't have built-in JWT token generation capabilities).

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda