ASP.NET Core Web API 授权期间生成 JWT 令牌时出错

Pro*_*Vak 12 c# authorization jwt asp.net-core-webapi

我有一个 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, 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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

错误就在这一行 -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>
Run Code Online (Sandbox Code Playgroud)

响应正文中包含令牌和刷新令牌的预期状态 200。

Moh*_*ani 16

它通过添加这个包为我解决了:

<PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="7.0.1" />
Run Code Online (Sandbox Code Playgroud)

我使用了@springy76的评论。


aks*_*eli 8

首先,您可能应该包括:

<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.0.0" />
Run Code Online (Sandbox Code Playgroud)

在你的.csproj。确保该version值正确,您将希望它符合其他 NuGet 包的要求。

如果这不起作用,则可能是由于运行时尝试删除它认为运行时已存在且正在项目中复制的程序集而导致的问题。

尝试将其添加到您的.csproj文件中

<PropertyGroup>
    <_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

您可以在此处找到该问题的更详细描述和解释


Jor*_*ers 5

我通过将“System.IdentityModel.Token.Jwt”从 7.0.0 降级到 6.32.3 来修复此问题。7.0.0 似乎与其他 IdentityModel 包不兼容。

  • @EirikH 在我的例子中它是 `Microsoft.AspNetCore.Authentication.OpenIdConnect` 7.0.11 https://www.nuget.org/packages/Microsoft.AspNetCore.Authentication.OpenIdConnect/7.0.11#dependency-body-tab 其中“加载”旧的 6.x 版本的“Microsoft.IdentityModel.Protocols.OpenIdConnect”,而该版本似乎使用了(内部!)JsonObjectAttribute 类,该类不再存在于“System.IdentityModel.Tokens”7.0.11 中。添加 `&lt;PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="7.0.0" /&gt;` 为我解决了这个问题 (14认同)
  • 这是一个[主要版本,有一些重大更改](https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet/wiki/IdentityModel-7x)。您可能需要更新代码,如果您使用依赖于 IdentityModel 的其他软件包,您需要等待它们也支持 7.x 版本。 (2认同)