title: c# Configuration class and KeyVault
body: <p>I just need this clarifying for me, maybe someone has had a similar situation and come up with a decent solution.
  Currently we store all our sensitive data in <strong>ApplicationSettings</strong> which is not great.
  And from the application settings we use <strong>Autofac</strong> to create a singleton <strong>Configuration</strong> class that is then injected into any class that requires any of the settings.</p>
  
  <p>This is what the class currently looks like:</p>
  
  <pre><code>public class CormarConfig : ICormarConfig
  {
      public bool Live =&gt; ConfigurationManager.AppSettings["Server"].ToUpper().Equals("LIVE");
      public string SqlConnectionString =&gt; ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString();
      public string OracleConnectionString =&gt; ConfigurationManager.ConnectionStrings["OracleConnection"].ToString();
  
      public connectionType ConnectionType =&gt; Live ? connectionType.Live : connectionType.Test;
      public string CollectionManagerEndpoint =&gt; ConfigurationManager.AppSettings["CollectionManagerEndpoint"];
      public string TradePortalUrl =&gt; ConfigurationManager.AppSettings["TradePortalUrl"];
      public string TroposDb =&gt; ConfigurationManager.AppSettings["TroposDatabase" ];
      public string WmsBaseEndpoint =&gt; ConfigurationManager.AppSettings["WmsBaseEndpoint"];
  
      /// ------- Removed from brevity ------- ///
  
  }
  </code></pre>
  
  <p>So, now we moved a lot of these sensitive settings into azure <strong>Key Vault</strong> which is good. I then created a simple class for retrieving these "secrets" like this:</p>
  
  <pre><code>public class SecretProvider : ISecretProvider
  {
      private readonly KeyVaultClient _keyVaultClient;
      private readonly ISecretConfig _config;
  
      public SecretProvider(ISecretConfig config)
      {
          var azureServiceTokenProvider = new AzureServiceTokenProvider();
          _keyVaultClient = new KeyVaultClient(
              new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
          _config = config;
      } 
  
      public async Task&lt;string&gt; GetAsync(string name)
      {
          var secret = await _keyVaultClient.GetSecretAsync($"{_config.KeyVaultEndPoint}/secrets/{name}");
          return secret.Value;
      }
  }
  </code></pre>
  
  <p>I have tested this and it works fine.
  The problem is I now what to replace some of the <strong>Properties</strong> in my <strong>CormarConfig</strong> class.</p>
  
  <p>For example, I would like to do something like:</p>
  
  <pre><code>private readonly ISecretProvider _secretProvider;
  public CormarConfig(ISecretProvider secretProvider) =&gt; _secretProvider = secretProvider;
  
  private string _sqlConnectionString;
  public async Task&lt;string&gt; GetSqlConnectionString()
  {
      if (!string.IsNullOrEmpty(_sqlConnectionString)) return _sqlConnectionString;
      _sqlConnectionString = await _secretProvider.GetAsync("DefaultConnection");
      return _sqlConnectionString;
  }
  </code></pre>
  
  <p>Would this be an acceptable solution?</p>