At the time you are calling services.AddSomething()
, the service provider has not been built from the service collection yet. So you cannot instantiate a service at that time. Fortunately, there is a way to configure services while using dependency injection.
When you do services.AddSomething(options => …)
what usually happens is that a certain amount of services will be registered with the service collection. And then the passed configuration action will also be registered in a special way, so that when the service is later instantiated, it will be able to execute that configuration action in order to apply the configuration.
For that, you need to implement IConfigureOptions<TOptions>
(or actually IConfigureNamedOptions<TOptions>
for authentication options) and register it as a singleton. For your purpose, this could look like this:
public class ConfigureJwtBearerOptions : IConfigureNamedOptions<JwtBearerOptions>
{
private readonly IMyService _myService;
public ConfigureJwtBearerOptions(IMyService myService)
{
// ConfigureJwtBearerOptionsis constructed from DI, so we can inject anything here
_myService = myService;
}
public void Configure(string name, JwtBearerOptions options)
{
// check that we are currently configuring the options for the correct scheme
if (name == JwtBearerDefaults.AuthenticationScheme)
{
options.TokenValidationParameters = myService.GetTokenValidationParameters();
}
}
public void Configure(JwtBearerOptions options)
{
// default case: no scheme name was specified
Configure(string.Empty, options);
}
}
You then register that type in your Startup
:
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
// add JwtBearer but no need to pass options here
.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, configureOptions: null);
// instead we are registering our configuration type to configure it later
services.AddSingleton<IConfigureOptions<JwtBearerOptions>, ConfigureJwtBearerOptions>();
This is actually the exact same thing that happens when you just do services.AddJwtBearer(scheme, options => { … })
, just abstracted away, so you don’t need to care about it. But by doing it manually, you now have more power and access to the full dependency injection service provider.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…