将身份验证从dotnet核心1.1迁移到dotnet核心2.0

Teo*_*vov 3 dependency-injection asp.net-authentication asp.net-core

我们只是将我们的身份验证中间件从.net核心1.1迁移到.net核心2.0,遵循答案中的示例.然而,当我尝试发出请求时(即使在尝试访问Swagger UI时),所有内容都会构建和运行.我在我的自定义AuthenticationHandler调用上得到以下异常UserAuthHandler : System.InvalidOperationException: A suitable constructor for type 'BrokerAPI.AuthMiddleware.UserAuthHandler' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.
代码来自UserAuthHandler:

public class UserAuthHandler : AuthenticationHandler<UserAuthAuthenticationOptions>    
{
    protected UserAuthHandler(IOptionsMonitor<UserAuthAuthenticationOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) : base(options, logger, encoder, clock)
    {
    }

    protected override Task<AuthenticateResult> HandleAuthenticateAsync()
    {
        //handle authentication
        var ticket = new AuthenticationTicket(new ClaimsPrincipal(identity),
           new AuthenticationProperties(), "UserAuth");

        return Task.FromResult(AuthenticateResult.Success(ticket));
    }
}
Run Code Online (Sandbox Code Playgroud)

代码来自UserAuthExtensions:

public static class UserAuthExtensions
{
    public static AuthenticationBuilder AddCustomAuth(this AuthenticationBuilder builder, Action<UserAuthAuthenticationOptions> configureOptions)
    { 
        return builder.AddScheme<UserAuthAuthenticationOptions, UserAuthHandler>("UserAuth", "UserAuth", configureOptions);
    }
}
Run Code Online (Sandbox Code Playgroud)

我如何称呼所有内容Startup.cs:

public void ConfigureServices(IServiceCollection services)
    {
        services.AddAuthentication(options =>
            {
                options.DefaultScheme = "UserAuth";
            }).AddCustomAuth(o => { });
    }
public void Configure()
    {
        app.UseAuthentication();
    }
Run Code Online (Sandbox Code Playgroud)

我已经搜索谷歌的例子和有类似问题的人,但无济于事.

我错过了与我的DI容器有关的东西吗?或者它是否与.net core 2中的身份验证有关?

提前致谢.

Kév*_*let 6

异常消息实际上非常清楚:您的处理程序有一个受保护的构造函数,依赖注入系统无法使用它.公开它应该工作:

public class UserAuthHandler : AuthenticationHandler<UserAuthAuthenticationOptions>    
{
    public UserAuthHandler(IOptionsMonitor<UserAuthAuthenticationOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) : base(options, logger, encoder, clock)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)