azure-activedirectory-identitymodel-extensions-for-dotnet: [Bug] Method not found Exception when reading JWT token (Base64UrlEncoder.UnsafeDecode)

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

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 2
  • Comments: 15 (3 by maintainers)

Most upvoted comments

Same error. Is there any news on this thread?

The error fixed by removing “Microsoft.IdentityModel.Tokens” packet.

Same error. Is there any news on this thread?

The error fixed by removing “Microsoft.IdentityModel.Tokens” packet.

Thank you, it also worked for me by removing “Microsoft.IdentityModel.Tokens” packet.

Another issue with mismatched versions

Same error. Is there any news on this thread?

The error fixed by removing “Microsoft.IdentityModel.Tokens” packet.

Removing the packages also worked for me to fix this error message. Thanks for the idea!


Some more information for anyone that runs into the Missing method error:

After removing any explicitly added Microsoft.IdentityModel.* NuGet packages from my WebAPI project, it worked again. I investigated the cause a little bit, and in my case these packages were already implicitly added by other NuGet packages. And upgrading the IdentityModel packages manually to a newer version apparently causes version conflicts with other included packages.

In my example, these were my implicit references to the Microsoft.IdentityModel.Tokens package: Image

Removing the manually added packages and recompiling did the trick for me, and still keeps all references working.

it worked for me, thanks

Thanks man. It solved my issue too. I spent 2 hours to fix it

Another issue with mismatched versions

Fixed by updating IdentityModel packages in the HttpApi.Host project as described here.

Same error. Is there any news on this thread?