Saeed Asgari
Saeed Asgari

Reputation: 427

Decoding Jwt in Blazore Client_side results WASM: System.ArgumentException: IDX12723: Unable to decode the payload '[PII is hidden

Im trying to decode a Jwt generated by my asp.net core api this is the code i use to decode my jwt

        var handler = new JwtSecurityTokenHandler();
        var tokenS = handler.ReadJwtToken(jwt);
        return tokenS;

This works in server. no problem. But if i try to use the same piece of code in Blazor client_side i get this error.

blazor.webassembly.js:1 WASM: System.ArgumentException: IDX12723: Unable to decode the payload '[PII is hidden. For more details, see https://aka.ms/IdentityModel/PII.]' as Base64Url encoded string. jwtEncodedString: '[PII is hidden. For more details, see https://aka.ms/IdentityModel/PII.]'. ---> Newtonsoft.Json.JsonSerializationException: Unable to find a default constructor to use for type System.IdentityModel.Tokens.Jwt.JwtPayload. Path 'unique_name', line 1, position 15.

this is the jwt token im using and trying to decode

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1bmlxdWVfbmFtZSI6IjA4ZDdjYzE5LWY2OTgtYTRlOS05ODcxLWE2ZWM1ODg1OTRhZCIsInJvbGUiOiJBZG1pbiIsImdpdmVuX25hbWUiOiJvY2xpY2swMjEiLCJuYmYiOjE1ODQ4MjM0MTYsImV4cCI6MTU4NDgyMzcxNiwiaWF0IjoxNTg0ODIzNDE2fQ.nD3YzBu1qvNelDz2WHcMSGcKkTtTHX98baNTBXeu12M

Upvotes: 3

Views: 2129

Answers (2)

Nicholas Petersen
Nicholas Petersen

Reputation: 9558

To clarify @saeed-asgari's answer, IdentityModelEventSource is a class in the Microsoft.IdentityModel.Logging namespace / package. Obviously this is a static member so it only needs to be set once. But for my purpose, I only needed this to be set true during a debug session (on encountering this problem), so I did this as follows: manually setting _debug_showPII to true while debugging (or better: commenting this out once no longer needed):

try {
    bool _debug_showPII = false;
    if(_debug_showPII)
        Microsoft.IdentityModel.Logging.IdentityModelEventSource.ShowPII = true;

    var tokenHandler = new JwtSecurityTokenHandler();

    ClaimsPrincipal principal = tokenHandler.ValidateToken(...)
    //...
}
catch(Exception ex) { /*...*/ }

I started getting this exception after updating a bunch of dependencies (from roughly .NET 6 to .NET 8), including some JWT related types, I may just need to update some other dependencies to fix this. That said, the error I was getting was also related to JSON deserialization, though I'm not currently sure why it should be a problem:

IDX12723: Unable to decode the payload 'eyJ...'* as Base64Url encoded string.

IDX11023: Expecting json reader to be positioned on 'JsonTokenType.String or JsonTokenType.StartArray', reader was positioned at: 'Number', Reading: 'System.IdentityModel.Tokens.Jwt.JwtPayload', Position: '41', CurrentDepth: '1', BytesConsumed: '47'.
  • Note: despite the claim of unable to parse Base64Url, #1 this token is purely alpha-numeric, and #2: it clearly was valid base64Url encoding. Proof being this was a unit test, and the string hasn't changed, which worked for years prior to this bump of versions.

UPDATE: This particular problem in my case was fixed by updating the package System.IdentityModel.Tokens.Jwt from 7.0.3 to 7.1.2. In Nuget, 7.0.3 was listed as having some kind of vulnerability, I wonder if this was just a bug (though that version was fine before other dependencies were updated).

<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.1.2" />

Upvotes: 0

Saeed Asgari
Saeed Asgari

Reputation: 427

OK for anybody who has this problem later. please dont be like me and first read the error. so for more details i turned the PII on. just put this line of code before error causing line.

   IdentityModelEventSource.ShowPII = true;

now i could see a better detail of my problem this was the error after turning my PII on

Newtonsoft.Json.JsonSerializationException: Unable to find a default constructor to use for type System.IdentityModel.Tokens.Jwt.JwtPayload

And after a little bit searching i this was my solution

Put this line of code in Program.cs after builder.Build(); like this

      var host = builder.Build();
        _ = new JwtPayload();

BOOM error is gone and you can deserialize your jwt and make awesome things.

Upvotes: 7

Related Questions