azure-activedirectory-identitymodel-extensions-for-dotnet
azure-activedirectory-identitymodel-extensions-for-dotnet copied to clipboard
[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
@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.
Hey @pmaytak, Yes, this list includes transitive packages and this is the full call stack related to the given exception.
hi @JFFby I am not able to reproduce this. Can you confirm that this doesn't repro on a clean new app?
Same error. Is there any news on this thread?
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
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?
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!
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.
Microsoft.IdentityModel.Tokens
Thanks dude i spent about 1 hour to fix it.
Thanks man. It solved my issue too. I spent 2 hours to fix it
it worked for me, thanks
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:
Removing the manually added packages and recompiling did the trick for me, and still keeps all references working.
Caused by this nuget, in my case:
<PackageReference Include="Microsoft.IdentityModel.Tokens.Saml" Version="8.7.0" />