認証ミドルウェアを.net core 1.1から.net core 2.0にマイグレーションしました。この例は、this answerの例に従っています。すべてがビルドされ、実行は、しかし、ときに私は私がUserAuthHandler
と呼ばれる私のカスタムAuthenticationHandler
上で、次の例外を取得(SWAGGER UIを取得しようとしているにも)要求をしよう: 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
からコード:dotnet core 1.1からdotnet core 2.0への認証の移行
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));
}
}
UserAuthExtensions
からコード:
public static class UserAuthExtensions
{
public static AuthenticationBuilder AddCustomAuth(this AuthenticationBuilder builder, Action<UserAuthAuthenticationOptions> configureOptions)
{
return builder.AddScheme<UserAuthAuthenticationOptions, UserAuthHandler>("UserAuth", "UserAuth", configureOptions);
}
}
私はStartup.cs
ですべてを呼び出す方法:
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(options =>
{
options.DefaultScheme = "UserAuth";
}).AddCustomAuth(o => { });
}
public void Configure()
{
app.UseAuthentication();
}
私は、同様の問題を抱えている例や人物についてGoogleを掃除しましたが、役に立たないものです。
DI容器に関連するものがありませんか?または、それは一般的に.netコア2の認証に関連するものですか?
ありがとうございます。