Search code examples
c#asp.netencryptionjwtjwe

ASP.NET In one JWE controller, the token comes with Headers, and in the other without Headers


The problem is that in one controller, this method gets all the headers when reading the token, and in the other controller not all the headers, although the methods, library versions, etc. are the same, here is the method itself:

[HttpGet(Name = "GetTokenPrincipal")]
[Route("/GetTokenPrincipal")]
public ClaimsPrincipal? GetTokenPrincipal([FromQuery]string token)
{  
        var jwtSecurityToken = new           System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler().ReadJwtToken(token);
        var tokenEnc = jwtSecurityToken.Header.Enc;

        throw new NotImplementedException();
}

Headers received from the first controller from jwe:

{"alg":"dir","enc":"A256CBC-HS512","typ":"JWT","cty":"JWT"}.{}

Headers received from the second controller from jwe:

{"alg":"dir","typ":"JWT"}.{}

What i try:

  1. Checked the work in two different controllers,
  2. Checked the connections in builder.Services and app.Use...

Solution

  • The problem was that the libraries had different versions:

    1. Microsoft.IdentityModel.Tokens (Version was 8.0.1) (Version is now 7.1.2)
      
    2. System.IdentityModel.Tokens.Jwt (Version was 7.1.2) (Version is now 7.1.2)
      

    Versions of these libraries must be the same, or everything stops working!!