Reputation: 5736
I am adding OpenIdConnect to my app like so:
.AddOpenIdConnect("oidc", options =>
{
var clientSecret = Configuration.GetValue<string>("clientSecret");
options.ClientSecret = clientSecret;
});
I'd like to be able to use another service to get the secret like this:
.AddOpenIdConnect("oidc", (services, options) =>
{
var secretService = services.GetService<ISecretService>();
var clientSecret = secretService.Get("clientSecret");
options.ClientSecret = clientSecret;
});
I saw there is use of app.UseOpenIdConnectAuthentication
but I don't see it in the nuget package.
I have the following installed:
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="3.1.0" />
How can I do this?
Upvotes: 1
Views: 1309
Reputation: 3027
In the described case I'd recommend extending Configuration rather than using DI in the Action.
To access secrets you can add Configuration providers and continue to use Configuration.GetValue in ConfigureServices method.
For Azure Key-Vault it is under Microsoft.Extensions.Configuration.AzureKeyVault nuget package.
public static void Main(string[] args)
{
CreateWebHostBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
if (env.IsLocal())
{
...
}
else
{
config.AddAzureKeyVault(keyVaultUri);
}
})
.Build()
.Run();
}
For AWS - Amazon.Extensions.Configuration.SystemsManager
Upvotes: 1
Reputation: 5736
It is possible to execute a post configuration class that can inject services. Like so:
public class OpenIdConnectPostConfigureOptions : IPostConfigureOptions<OpenIdConnectOptions>
{
private readonly ISecretsService _secretsService;
public OpenIdConnectPostConfigureOptions(ISecretsService secretsService)
{
_secretsService = secretsService;
}
public async void PostConfigure(string name, OpenIdConnectOptions options)
{
options.ClientSecret = await _secretsService.Get("clientSecret");
}
}
Upvotes: 1