I have an object value in the key vault key of azure and trying to read the value at the startup class but the value is always null.
Program.cs
var azureServiceTokenProvider = new AzureServiceTokenProvider();
var keyVaultClient = new KeyVaultClient(
new KeyVaultClient.AuthenticationCallback(
azureServiceTokenProvider.KeyVaultTokenCallback));
config.AddAzureKeyVault(
vaultConfig.AzureVaultUrl,
keyVaultClient,
new DefaultKeyVaultSecretManager());
Startup.cs
public virtual void ConfigureSettings(IServiceCollection services)
{
services.AddOptions();
services.Configure<OktaConfig>(Configuration.GetSection("OktaConfig")); // Read the value from Azure Key Vault or %APPDATA%\Microsoft\UserSecrets\<user_secrets_id>\secrets.json for Dev Environment
}
Controller.cs
public class SystemController
{
private readonly IConfiguration _config;
private readonly OktaConfig _oktaConfig;
public SystemController(IConfiguration config, IOptions<OktaConfig> oktaConfig)
{
_config = config;
_oktaConfig = oktaConfig.Value; // This value is null
}
[HttpGet("Vaults")]
public IActionResult GetVaultValues()
{
var OktaConfig = JsonConvert.DeserializeObject<OktaConfig>(_config["OktaConfig"]);
return Ok(OktaConfig);
}
The _oktaConfig value is always null.
Here is my azure key vault value
Json structure of key secret value
{
"OktaDomain": "https://dev-166545.okta.com/oauth2/XXXXXXXXXXXXXXXXX/v1/authorize",
"ClientId": "XXXXXXXXXXXXXXXXXXXXXXXXXX",
"ClientSecret": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"redirectUrl": "https://localhost:44307"
}
ViewModel.cs
public class OktaConfig
{
public string RedirectUrl { get; set; }
public string OktaDomain { get; set; }
public string ClientId { get; set; }
public string ClientSecret { get; set; }
public string ClientName { get; set; }
}
With this _config["OktaConfig"] code I am able to get the value, but I want to do dependency injection for that.