Skip to content

Instantly share code, notes, and snippets.

@configureappio
configureappio / HomeController.cs
Created March 28, 2018 18:17
Example of change to controller to take IMyAppSettings as the parameter instead of IOptionsSnapshot<MyAppSettings>
public class HomeController : Controller
{
private readonly IMyAppSettings _settings;
public HomeController(IMyAppSettings settings)
{
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
}
public IActionResult Index()
{
@configureappio
configureappio / Startup.cs
Created April 2, 2018 15:38
Example of injecting a decyption service and settings validator into the settings bridge
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddOptions();
services.Configure<MyAppSettings>(Configuration.GetSection("MyAppSettings"));
services.AddSingleton(Configuration);
// In a real implementation, you would have a factory here to get the password and salt securely
// from somewhere such as Azure Key Vault, Environmental variable / another json setting (obfuscated in some way)
services.AddSingleton( x => new CryptoFactory().Create<AesManaged>("Password", "Salt"));
@configureappio
configureappio / Program.cs
Created April 2, 2018 15:56
Example of using a PhysicalFileProvider to access a JSON file outside of the web site
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args)
{
var webhost = WebHost.CreateDefaultBuilder(args);
@configureappio
configureappio / appsettings.json
Created April 2, 2018 16:08
Hiding secrets in appsettings.json
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
},
"MyAppSettings": {
"ApplicationName": "My Cool Application",
"Secrets": {
@configureappio
configureappio / MyAppSettings.,cs
Created April 2, 2018 16:13
Example of class to read secrets dictionary
public class MyAppSettings : IAppSettingsStructure
{
public string ApplicationName { get; set; }
public Dictionary<string, string> Secrets { get; set; } = new Dictionary<string, string>();
}
@configureappio
configureappio / ICryptoFactory.cs
Last active April 2, 2018 16:27
An example factory interface for creating a decryptor
public interface ICryptoFactory
{
ICryptoAlgorithm Create<T>(string password, string salt) where T : SymmetricAlgorithm, new();
ICryptoAlgorithm Create(Type type, string password, string salt);
}
public interface ICryptoAlgorithm
{
string Decrypt(string text);
string Encrypt(string text);
@configureappio
configureappio / SettingsDecryptor.cs
Created April 2, 2018 16:37
Example of decrypting a dictionary with a hashed key
public class SettingsDecryptor : ISettingsDecrypt
{
private readonly ICryptoAlgorithm _crypto;
public SettingsDecryptor(ICryptoAlgorithm crypto)
{
_crypto = crypto ?? throw new ArgumentNullException(nameof(crypto));
}
public string Decrypt(string key, IDictionary<string, string> keyValues)
{
@configureappio
configureappio / SettingsValidator.cs
Created April 3, 2018 07:26
Example of configuration settings validator
public class SettingsValidator : ISettingsValidator
{
public bool TryValidate(IAppSettingsStructure settings, out AggregateException validationExceptions)
{
if (settings == null) throw new ArgumentNullException(nameof(settings));
var exceptions = new List<Exception>();
if (settings.ApplicationName == null) exceptions.Add(new ArgumentNullException(nameof(settings.ApplicationName)));
if (string.IsNullOrWhiteSpace(settings.ApplicationName)) exceptions.Add(new ArgumentOutOfRangeException(nameof(settings.ApplicationName)));
@configureappio
configureappio / MyAppSettingsBridge.cs
Created April 3, 2018 07:36
Example of bridging settings class that takes a validator and and decryptor
public class MyAppSettingsBridge : IAppSettingsResolved
{
private readonly IOptions<MyAppSettings> _appSettings;
private readonly ISettingsDecrypt _decryptor;
public MyAppSettingsBridge(IOptionsSnapshot<MyAppSettings> appSettings, ISettingsDecrypt decryptor, ISettingsValidator validator) {
_appSettings = appSettings ?? throw new ArgumentNullException(nameof(appSettings));
_decryptor = decryptor ?? throw new ArgumentException(nameof(decryptor));
if (validator == null) throw new ArgumentNullException(nameof(validator));
@configureappio
configureappio / Interfaces.cs
Created April 3, 2018 07:48
An example of aggregating multiple interfaces to allow registration with interface segregation
public interface IAppSettings
{
string ApplicationName { get; }
}
public interface ISqlConnectionSettings
{
string SqlConnectionSting { get; }
}