Skip to content

Instantly share code, notes, and snippets.

@kamranayub
Created February 24, 2016 00:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamranayub/eb6518356ac2b2f1a72a to your computer and use it in GitHub Desktop.
Save kamranayub/eb6518356ac2b2f1a72a to your computer and use it in GitHub Desktop.
Secrets provider interface
public class ConfigSecretsProvider : ISecretsProvider
{
private readonly NameValueCollection _secretCollection;
public ConfigSecretsProvider() {
_secretCollection = ConfigurationManager.GetSection("appSecrets") as NameValueCollection;
}
public string GetSecret(string key) {
return Environment.GetEnvironmentVariable(key) ?? _secretCollection[key];
}
public Task<string> GetSecretAsync(string key)
{
return Task.FromResult(GetSecret(key));
}
}
public interface ISecretsProvider
{
/// <summary>
/// Retrieves a secret by key
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
string GetSecret(string key);
/// <summary>
/// Retrieves a secret by key (async)
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
Task<string> GetSecretAsync(string key);
}
<!-- Example -->
<configuration>
<configSections>
<section name="appSecrets" type="System.Configuration.NameValueSectionHandler" />
</configSections>
<appSettings>
<!-- No longer has secrets! -->
</appSettings>
<appSecrets>
<add key="FooSecret" value="SuperSecret"/>
</appSecrets>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment