我有一个Web API项目,我通过它注册和登录.在我应该获得令牌的授权过程中,出现错误: 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'

令牌生成代码:

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, Security算法rithms.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在这条线上--return new JwtSecurityTokenHandler().WriteToken(tokenOptions);

已连接的套餐:

<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>

响应正文中应有令牌和刷新令牌的状态200.

推荐答案

首先,您可能应该包括:

<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.0.11" />

在你的.csproj美元里.确保version值是正确的,您会希望它与其他NuGet包中的要求相匹配.

如果这不起作用,这可能是运行库try 移除它认为运行库已经拥有并且正在项目中复制的程序集所导致的问题.

try 将此内容添加到您的.csproj文件

<PropertyGroup>
    <_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
</PropertyGroup>

你可以找到关于这个问题的更详细的描述和解释

Csharp相关问答推荐

Microsoft.AspNetCore.Mvc. Controller Base.用户:属性或索引器Controller Base.用户无法分配给--它是只读的

WPF Windows初始化正在锁定. Net 8中分离的线程

try 还原包时出错:Dapper已经为System.Data.SQLClient定义了依赖项''''

安装附加的. exe与Visual Studio

碰撞检测与盒碰撞器,其isTrigger on问题

Blazor EventCallback<;MyType<;T>;>;

Azure函数中实体框架核心的依赖注入

如何让NLog停止写入冗余信息?

如何将MemberInitExpression添加到绑定中其他Lambda MemberInitExpression

EFR32BG22 BLE在SPP模式下与PC(Windows 10)不连接

Polly重试URL复制值

为什么@rendermode Interactive Auto不能在.NET 8.0 Blazor中运行?

如何在.NET AOT中为所有枚举启用JsonStringEnumConverter

C#动态设置ServerReport报表参数

为什么C#/MSBuild会自发地为不同的项目使用不同的输出路径?

如何在使用属性 Select 器时判断是否可以为空

用于请求用户返回列表的C#Google API

VS代码扩展无法在新版本扩展C#中运行从v2.10.28开始

Windows 10上埃及标准时间的时区偏移不正确

默认架构不存在EF核心迁移