小编Pro*_*Vak的帖子

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

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

c# authorization jwt asp.net-core-webapi

12
推荐指数
3
解决办法
5794
查看次数

标签 统计

asp.net-core-webapi ×1

authorization ×1

c# ×1

jwt ×1