Search code examples
c#asp.net-corejwt.net-8.0

IDX12723: Unable to decode the payload '[PII of type 'System.String' is hidden


After I upgrade my project to .NET 8 from .NET 6, I faced this error:

System.ArgumentException: 'IDX12723: Unable to decode the payload '[PII of type 'System.String' is hidden. For more details, see https://aka.ms/IdentityModel/PII.]' as Base64Url encoded string.'
Inner Exception
JsonException: IDX11020: The JSON value of type: 'String', could not be converted to 'JsonTokenType.Number'. Reading: 'System.IdentityModel.Tokens.Jwt.JwtPayload.iat', Position: '52', CurrentDepth: '1', BytesConsumed: '82'.

I tried many ways but I can not solve it. The error appreared when go here:

var token = filterContext.HttpContext.Request.Headers["Authorization"].FirstOrDefault()?.Split(" ")[1];
var tokenHandler = new JwtSecurityTokenHandler();
var authToken = tokenHandler.ReadToken(token) as JwtSecurityToken; // <=== ERROR HERE

Please help me! Thank you so much!


Solution

  • JsonException: IDX11020: The JSON value of type: 'String', could not be converted to 'JsonTokenType.Number'. Reading: 'System.IdentityModel.Tokens.Jwt.JwtPayload.iat', Position: '52', CurrentDepth: '1', BytesConsumed: '82'.

    See this comment to this issue @github:

    Based on the RFC 7519 this claim MUST be number.

    The JWT that you are sending in the request has iat claim as string and the later version of the library is stricter about this. Without seeing the actual value (and how you are getting it) you can try:

    UPD

    Based on the comments the last option applied - explicit DateTimeOffset.Now.ToString() value was set to JwtPayload.iat header. DateTimeOffset.Now.ToUnixTimeSeconds() was suggested as substitution, another option is EpochTime.GetIntDate(DateTime) (see this comment).