user2209634
user2209634

Reputation: 649

Error when attempting to decode JSON Web Token returned from own backend API -

I have a Blazor WebAssembly application with an ASP.Net Core Web API backend.

The app uses JSON Web Tokens, which are created and returned by the backend API as part of a successful authentication by the user.

I have a problem once the JWT has been returned to the browser, and Blazor is attempting to decode the contents of the token.

In my development environment, this works beautifully, and decodes all the encoded information within the token.

However, when attempting to run both the API and Blazor Webassembly front end on the web server, the following exception is generated when attempting to decode the token:

EXCEPTION: IDX12729: Unable to decode the header 'System.String' as Base64Url encoded string. jwtEncodedString: 'System.String'

The exception is generated by the following code:

var jwtHandler = new JwtSecurityTokenHandler();

var jwtToken = jwtHandler.ReadJwtToken(jwtTokenString);

System.IdentityModel.Tokens.Jwt, Version=6.8.0.0

The actual token is below:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiam9obmRvZSIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL25hbWVpZGVudGlmaWVyIjoiZTUwZjZhYmUtYjcyYi00MWI2LWIwYTctMGE3Y2M0OGYwZWYzIiwiZ2l2ZW5fbmFtZSI6IkpvaG4iLCJmYW1pbHlfbmFtZSI6IkRvZSIsImVtYWlsIjoiam9obkBkb2UuY29tIiwiYXV0aF90aW1lIjoiMDcvMDMvMjAyMSAyMTo0MTozMCIsImV4cCI6MTYxNTE4OTI5MCwiQ29tcGFueUlkIjoiMSIsIkNvbXBhbnlOYW1lIjoiSkQgQ28gUExDIiwiT3JnYW5pc2F0aW9uSWQiOiIxIiwiT3JnYW5pc2F0aW9uTmFtZSI6IkpEIE9yZyBQTEMiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL3JvbGUiOlsiQ29tcGFueUFkbWluIiwiR2VuZXJhbFVzZXIiLCJHbG9iYWxBZG1pbiIsIk9yZ2FuaXNhdGlvbkFkbWluIiwiT3JnYW5pc2F0aW9uQmlsbGluZyJdLCJpc3MiOiJKb2huRG9lQXBpIiwiYXVkIjoiaHR0cHM6Ly9qb2huZG9lLmNvbSJ9.klbdvnjikSbnjr8q_7d4vxMsxajPJ3nY3yiDevJCdHo

I have validated the token (via https://devtoolzone.com/decoder/jwt ) and everything looks ok.

Contents of Token

Can anyone offer any guidance?

Upvotes: 0

Views: 3481

Answers (1)

user2209634
user2209634

Reputation: 649

On further investigation, there was an Inner Exception that I hadn't spotted which read:

Unable to find a default constructor to use for type System.IdentityModel.Tokens.Jwt.JwtHeader

This lead me to this known issue on Github: https://github.com/mono/linker/issues/870

In summary, there is a workaround, by including the following code in program.cs after builder.Build():

_ = new JwtHeader();
_ = new JwtPayload();

I don't begin to understand what the issue is, but am just happy that now everything works as expected.

Upvotes: 1

Related Questions