0

anyone else encountered this - you read your secrets from an azure key vault (typically in Program.cs right? Like this:

                .ConfigureAppConfiguration(builder =>
                {
                    var configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
                    var keyVaultEndpoint = configuration.GetSection("KeyVault").GetSection("KeyVaultEndpoint").Value);
                    var azureServiceTokenProvider = new AzureServiceTokenProvider();
                    var keyVaultClient = new KeyVaultClient(
                        new KeyVaultClient.AuthenticationCallback(
                            azureServiceTokenProvider.KeyVaultTokenCallback));
                    builder.AddAzureKeyVault(keyVaultUrl, keyVaultClient, new DefaultKeyVaultSecretManager());

                    var secretValue = Task.Run(async () => await keyVaultClient.GetSecretAsync(keyVaultEndpoint, "SomeDatabasePassword")).Result.Value;

How do you use this secretValue somewhere else, for example in a controller? Considering my appsettings looks like this:

{
    "KeyVault": {
               "SomeDatabasePassword": ""
     }
}

typically empty because we don't want to be explicitly set here in appsettings. But in Program.cs I can read that element from secrets and (while still in Program.cs) I can overwrite the appsettings key right? Like this: configuration.GetSection("KeyVault").GetSection("SomeDatabasePassword").Value = secretValue;

However if I then try to read this value configuration.GetSection("KeyVault").GetSection("SomeDatabasePassword").Value from a controller, I still get an empty string.

Any ideas?

Sami
  • 113
  • 3
  • 12
  • 1
    After modifying the value, it is only modified in memory, you need to write/ save it back to disk before you can read it again in another part of your program. Alternatively, you could also register a service that reads, and if it isn't set, sets that value and register it for [dependency injection](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-3.1) – MindSwipe Jun 15 '20 at 10:58
  • Please refer to https://stackoverflow.com/questions/59703912/how-to-let-ioptionsmonitort-get-the-latest-configuration-value-from-a-running/59744872#59744872 – Jim Xu Jun 16 '20 at 01:49

0 Answers0