'Decompress JWT payload in ASP.NET Core
I am setting up an ASP.NET Core Web API to use JWT Bearer authorization. It's built with .NET 5 and uses the Nuget package Microsoft.AspNetCore.Authentication.JwtBearer.
The code in Startup.cs looks like this
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options =>
{
options.Authority = "...";
options.Audience = "...";
options.Events = new JwtBearerEvents
{
OnTokenValidated = (context) =>
{
return Task.CompletedTask;
},
OnAuthenticationFailed = (context) =>
{
return Task.CompletedTask;
},
};
});
The OnAuthenticationFailed event occurs, and the context has an Exception property with the Message:
IDX12723: Unable to decode the payload 'System.String' as Base64Url encoded string. jwtEncodedString: ''.
That Exception has an InnerException property with the Message:
Unexpected character encountered while parsing value: U. Path '', line 0, position 0.
When I paste my JWT token into https://jwt.io/ I can see HEADER:
{
"typ": "JWT",
"zip": "DEF",
"kid": "b/O6OvVv1+y+WgrH5Ui9WTioLt0=",
"alg": "RS256"
}
PAYLOAD:
UQ[...very long binary looking string]
I find it suspicious that the InnerException message mentioned 'U', which is the first letter of my deflate compressed payload. It looks like the payload isn't being decompressed.
I wrote a small (separate) program to split the JWT between '.'s and then used a DeflateStream to retrieve the contents of the token's payload, which are - as I expected - a set of claims in valid JSON encoded in UTF-8.
Is Microsoft.AspNetCore.Authentication.JwtBearer unable to decompress token payloads? Is there an option I need to check to enable decompression? Or is there another library that will do this for me? Or can I even extend it myself, seeing as I am able to access the payload manually?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
