azure-activedirectory-identitymodel-extensions-for-dotnet icon indicating copy to clipboard operation
azure-activedirectory-identitymodel-extensions-for-dotnet copied to clipboard

[Bug] Method not found Exception when reading JWT token (Base64UrlEncoder.UnsafeDecode)

Open JFFby opened this issue 3 months ago • 3 comments

Which version of Microsoft.IdentityModel are you using? 7.5.1

Where is the issue?

  • M.IM.JsonWebTokens

Is this a new or an existing app?

Repro

I am just trying to add JWT authentication to the existed app

services.AddAuthentication()
.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options =>
{
    options.Audience = apiName;
    options.Authority = identityServerUrl;
});

Expected behavior Acess token successfully validated

Actual behavior A clear and concise description of what happens, e.g. an exception is thrown, UI freezes.

Possible solution I am getting exception:

IDX14100: JWT is not well formed, there are no dots (.).
The token needs to be in JWS or JWE Compact Serialization Format. (JWS): 'EncodedHeader.EndcodedPayload.EncodedSignature'. (JWE): 'EncodedProtectedHeader.EncodedEncryptedKey.EncodedInitializationVector.EncodedCiphertext.EncodedAuthenticationTag'.

Which has inner one with message:

Method not found: 'Byte[] Microsoft.IdentityModel.Tokens.Base64UrlEncoder.UnsafeDecode(System.ReadOnlyMemory`1<Char>)'.

With call stack:

   at Microsoft.IdentityModel.JsonWebTokens.JsonWebToken.ReadToken(String encodedJson)
   at Microsoft.IdentityModel.JsonWebTokens.JsonWebToken..ctor(String jwtEncodedString)
   at Microsoft.IdentityModel.JsonWebTokens.JsonWebTokenHandler.ReadToken(String token, TokenValidationParameters validationParameters)

Additional context / logs / screenshots / links to code

I double checked all dependencies, and they look good for me.

   [net8.0]:
   Top-level Package                                      Requested   Resolved
   > Microsoft.AspNetCore.Authentication.JwtBearer        8.0.4       8.0.4
   > Microsoft.Extensions.DependencyInjection             8.0.0       8.0.0
   > Microsoft.IdentityModel.JsonWebTokens                7.5.1       7.5.1
   > Microsoft.IdentityModel.Protocols.OpenIdConnect      7.5.1       7.5.1
   > Microsoft.IdentityModel.Tokens                       7.5.1       7.5.1
   > OpenIddict.Abstractions                              5.5.0       5.5.0
   > StyleCop.Analyzers                                   1.1.118     1.1.118
   > System.IdentityModel.Tokens.Jwt                      7.5.1       7.5.1

   Transitive Package                                           Resolved
   > Microsoft.Extensions.DependencyInjection.Abstractions      8.0.1
   > Microsoft.Extensions.Primitives                            8.0.0
   > Microsoft.IdentityModel.Abstractions                       7.5.1
   > Microsoft.IdentityModel.Logging                            7.5.1
   > Microsoft.IdentityModel.Protocols                          7.5.1

JFFby avatar May 01 '24 13:05 JFFby