我必须添加我们的自定义基本身份验证处理程序,但是我们的实现需要向其传递参数。我无法使用DI来实现。这是代码的一部分:
// Part of ConfigureServices in Startup.cs
public IServiceProvider ConfigureServices(IServiceCollection services)
{
...
// Some DIs in ConfigureService
services.AddTransient<ICustomService,CustomService>(); //this should be passed later to handler
services.AddHttpContextaccessor();
...
// Adding authentication in ConfigureServices
services.AddAuthentication("Basic")
.AddScheme<CustomBasicAuthenticationOptions,CustomBasicAuthenticationHandler>(
"Basic",options => new CustomBasicAuthenticationOptions()
{
CustomService = ??? // HOW OT GET CustomService: ICustomService from Container here?
});
...
}
// CustomBasicAuthenticationOptions
public class CustomBasicAuthenticationOptions : AuthenticationSchemeOptions
{
public CustomBasicAuthenticationOptions();
public static string AuthenticationScheme { get; }
public ICustomService CustomService { get; set; } //THIS SHOULD BE INJECTED HERE during AddAuthentication?! How?
...
}
// CustomService and its constructor of
public class CustomService : ICustomService
{
public CustomService(IHttpContextaccessor httpContextaccessor) {
...
}
}