JwtSecurityTokenHandler未在IoC中注册

时间:2019-09-09 07:27:41

标签: c# asp.net-core .net-core autofac

我在asp.net core 2.2中有以下代码:

FgBarcode::
    select(DB::raw('SUM(substr(fg_code, 22,14)) AS fg_code'))
    ->groupBy(DB::raw('substr(fg_code,2,3)'));

当我尝试运行项目时,出现以下错误:

public class JwtTokenManager : IJwtTokenManager
{
    private readonly JwtBearerSettings jwtBearerSettings;
    private readonly JwtSecurityTokenHandler jwtSecurityTokenHandler;

    public JwtTokenManager(IOptions<JwtBearerSettings> options,
                            JwtSecurityTokenHandler jwtSecurityTokenHandler)
    {
        this.jwtSecurityTokenHandler = jwtSecurityTokenHandler;
        jwtBearerSettings = options.Value;
    }

有人知道我如何注册Autofac.Core.DependencyResolutionException: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'IdentityTemplate.Infrastructure.Security.Jwt.JwtTokenManager' can be invoked with the available services and parameters: Cannot resolve parameter 'System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler jwtSecurityTokenHandler' of constructor 'Void .ctor(Microsoft.AspNetCore.Identity.UserManager`1[IdentityTemplate.Domain.Users.CustomUser], Microsoft.Extensions.Options.IOptions`1[IdentityTemplate.Infrastructure.Security.Jwt.JwtBearerSettings], System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler)'. 吗?

更新(添加了startup.cs)

JwtSecurityTokenHandler

1 个答案:

答案 0 :(得分:1)

用于Jwt授权的AddJwtBearer注册Authentication Scheme不少。如果要在类中注入JwtSecurityTokenHandler以进行测试,只需将其在asp.net核心内置容器中注册到 ConfigureService 方法中,否则就可以直接对其进行新建。

services.AddScoped<JwtSecurityTokenHandler>();