I have a Web API project through which I register and log in. During authorization, where I should get a token, an error appears:
An unhandled exception has occurred while executing the request. System.TypeInitializationException: The type initializer for System.IdentityModel.Tokens.Jwt.JsonExtensions' threw an exception. System.TypeLoadException: Could not load type 'Microsoft.IdentityModel.Json.JsonConvert' from assembly 'Microsoft.IdentityModel.Tokens, Version=7.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
Token generation code:
using ASFT.Auth.Interfaces;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Security.Cryptography;
namespace ASFT.Auth.Services
{
public class TokenService : ITokenService
{
public string GenerateAccessToken(IEnumerable<Claim> claims)
{
var signinCredentials = new SigningCredentials(AuthOptions.Key, SecurityAlgorithms.HmacSha256);
var tokenOptions = new JwtSecurityToken(
issuer: AuthOptions.Issuer,
audience: AuthOptions.Audience,
claims: claims,
expires: DateTime.Now.AddHours(48),
signingCredentials: signinCredentials);
return new JwtSecurityTokenHandler().WriteToken(tokenOptions);
}
}
}
The error is on this line - return new JwtSecurityTokenHandler().WriteToken(tokenOptions);
Connected packages:
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.11" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.11" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="7.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.11">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets></PackageReference>
Expected status 200 with token and refresh token in the response body.
It fixed for me by adding this package:
<PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="7.0.1" />
I used from comment of @springy76.
First of all, you should probably include:
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.0.0" />
in your .csproj. Make sure that the version value is correct, you're going to want it to match the requirements from your other NuGet packages.
If this doesn't work, this may be an issue caused by the runtime attempting to remove assemblies that it believes that the runtime already has and are being duplicated in your project.
Try adding this to your .csproj file
<PropertyGroup>
<_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
</PropertyGroup>
You can find a more detailed description and explanation of the issue here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With