This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HomeController : Controller | |
{ | |
private readonly IMyAppSettings _settings; | |
public HomeController(IMyAppSettings settings) | |
{ | |
_settings = settings ?? throw new ArgumentNullException(nameof(settings)); | |
} | |
public IActionResult Index() | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
BuildWebHost(args).Run(); | |
} | |
public static IWebHost BuildWebHost(string[] args) | |
{ | |
var webhost = WebHost.CreateDefaultBuilder(args); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Logging": { | |
"IncludeScopes": false, | |
"LogLevel": { | |
"Default": "Warning" | |
} | |
}, | |
"MyAppSettings": { | |
"ApplicationName": "My Cool Application", | |
"Secrets": { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyAppSettings : IAppSettingsStructure | |
{ | |
public string ApplicationName { get; set; } | |
public Dictionary<string, string> Secrets { get; set; } = new Dictionary<string, string>(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IAppSettings | |
{ | |
string ApplicationName { get; } | |
} | |
public interface ISqlConnectionSettings | |
{ | |
string SqlConnectionSting { get; } | |
} |