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 1 year ago • 3 comments
trafficstars

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

@JFFby Does that dependency list include transitive packages?

dotnet list yourcsrpoj.csproj package --include-transitive

Can you provide the full call stack?

This may be related to #2504 but that method was internal.

pmaytak avatar May 02 '24 16:05 pmaytak

Hey @pmaytak, Yes, this list includes transitive packages and this is the full call stack related to the given exception.

JFFby avatar May 03 '24 13:05 JFFby

hi @JFFby I am not able to reproduce this. Can you confirm that this doesn't repro on a clean new app?

keegan-caruso avatar May 06 '24 17:05 keegan-caruso

Same error. Is there any news on this thread?

mesuttalebiacerpro avatar Aug 01 '24 13:08 mesuttalebiacerpro

Same error. Is there any news on this thread?

The error fixed by removing "Microsoft.IdentityModel.Tokens" packet.

mesuttalebiacerpro avatar Aug 01 '24 13:08 mesuttalebiacerpro

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.

llgiant avatar Aug 12 '24 06:08 llgiant

Another issue with mismatched versions

keegan-caruso avatar Aug 13 '24 01:08 keegan-caruso

Another issue with mismatched versions

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

quirinoflavio avatar Aug 28 '24 23:08 quirinoflavio

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.

Removing the packet also fixed it for me. Thanks!

apkowalewski avatar Sep 09 '24 14:09 apkowalewski

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.

Removing the packet also fixed it for me. Thanks!

Literally just found this, also. The packet apparently has some extension with a bug in it.

danielphelps avatar Sep 09 '24 14:09 danielphelps

Microsoft.IdentityModel.Tokens

Thanks dude i spent about 1 hour to fix it.

Helius01 avatar Feb 14 '25 19:02 Helius01

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

nitinjaswal avatar Feb 23 '25 13:02 nitinjaswal

it worked for me, thanks

f-rezanejad avatar Mar 01 '25 10:03 f-rezanejad

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.

lanedirt avatar Mar 14 '25 12:03 lanedirt

Caused by this nuget, in my case:

    <PackageReference Include="Microsoft.IdentityModel.Tokens.Saml" Version="8.7.0" />

GuyHarel-Dev avatar Apr 11 '25 19:04 GuyHarel-Dev