I have to add our custom Basic Authentication Handler, but our implementation requires to pass parameter to it. I am not able to achieve this using DI. Here is part of the code:
// 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) {
...
}
}