Why doesn't AddCors or AddJwtBearer have support for getting a registered DI/IoC item? #57750
-
Hi folks, So we have for example: The allowed origins are defined in configuration files/environment variables. Likewise with setting up authentication's AddJwtBearer .. we have this: again, some settings via configuration. I couldn't find any way to grab this from the existing DI/IoC if we have previously registered it. My guess is that the above code is still being handled/called during registration of all our services. So could this be added to the code? Would this mean that cors and jwtsettings would be setup upon first instansiation / request of the instance/data? It's like I wish to load all configuration data before configuring any services .. but this problem is that i load configuration settings during service configuration (into so confused :( |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
Those are just the initial setting values, you can still configure the values after the calls to |
Beta Was this translation helpful? Give feedback.
-
Something like the below (typing from mobile so may not be exactly correct). You can inject any DI service, including public sealed class ConfigureMyJwtOptions(ISomeService myService) : IPostConfigureOptions<JwtBearerOptions>
{
public void PostConfigure(string name, JwtBearerOptions opts)
{
// limit to the specific JWT registration since you can technically have multiples by
// checking the name matches the one you used in the call to AddJwtBearer.
// If you don't specify one it uses the name from JwtBearerDefaults.
// Or you can ignore the name parameter and apply to all JetBearerOptions
// if (name == JwtBearerDefaults.AuthenticationScheme) {...}
opts.TokenValidationParameters.ValidIssuer = myService.GetIssuer();
}
}
// in startup
services.ConfigureOptions<ConfigureMyJwtOptions>(); Or implement |
Beta Was this translation helpful? Give feedback.
Those are just the initial setting values, you can still configure the values after the calls to
AddCors
/AddJwtBearer
but before the service provider is built. Just registerIConfigureOptions<CorsOptions>
orIConfigureOptions<JwtBearerOptions>
and inject your configuration dependencies into those. You can then useConfigure
(orPostConfigure
withIPostConfigureOptions<>
) to complete the configuration using your dynamic values