.netjwtblazorwebassemblyblazor-client-side

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


Solution

  • 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.