Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. public class Program
  2. {
  3. private const string APP_NAME = "5E71EE95-49BD-40A9-81CD-B1DFD873EEA8";
  4. private const string SECRET_CONFIG_FILE_NAME = "appsettings.secret.json";
  5.  
  6. public static void Main(string[] args)
  7. {
  8. if (args != null && args.Length == 1 && args[0].ToLowerInvariant() == "-config")
  9. {
  10. ConfigAppSettingsSecret();
  11. return;
  12. }
  13. CreateWebHostBuilder(args).Build().Run();
  14. }
  15.  
  16. public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
  17. WebHost.CreateDefaultBuilder(args)
  18. .ConfigureAppConfiguration((builder, options) =>
  19. {
  20. options.AddJsonFile(ConfigFileFullPath, optional: true, reloadOnChange: false);
  21. })
  22. .UseStartup<Startup>();
  23.  
  24. private static void ConfigAppSettingsSecret()
  25. {
  26. var serviceCollection = new ServiceCollection();
  27. AddDataProtection(serviceCollection);
  28. var services = serviceCollection.BuildServiceProvider();
  29. var dataProtectionProvider = services.GetService<IDataProtectionProvider>();
  30. var protector = CreateProtector(dataProtectionProvider);
  31.  
  32. string dbPassword = protector.Protect("DbPassword", ReadPasswordFromConsole());
  33. ... // other secrets
  34. string json = ...; // Serialize encrypted secrets to JSON
  35. var path = ConfigFileFullPath;
  36. File.WriteAllText(path, json);
  37. Console.WriteLine($"Writing app settings secret to '${path}' completed successfully.");
  38. }
  39.  
  40. private static string CurrentDirectory
  41. {
  42. get { return Directory.GetParent(typeof(Program).Assembly.Location).FullName; }
  43. }
  44.  
  45. private static string ConfigFileFullPath
  46. {
  47. get { return Path.Combine(CurrentDirectory, SECRET_CONFIG_FILE_NAME); }
  48. }
  49.  
  50. internal static void AddDataProtection(IServiceCollection serviceCollection)
  51. {
  52. serviceCollection.AddDataProtection()
  53. .SetApplicationName(APP_NAME)
  54. .DisableAutomaticKeyGeneration();
  55. }
  56.  
  57. internal static IDataProtector CreateProtector(IDataProtectionProvider dataProtectionProvider)
  58. {
  59. return dataProtectionProvider.CreateProtector(APP_NAME);
  60. }
  61. }
  62.  
  63. public void ConfigureServices(IServiceCollection services)
  64. {
  65. Program.AddDataProtection(services);
  66. ...
  67. }
  68.  
  69. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  70. {
  71. ...
  72. if (env.IsProduction())
  73. {
  74. var dataProtectionProvider = app.ApplicationServices.GetService<IDataProtectionProvider>();
  75. var protector = Program.CreateProtector(dataProtectionProvider);
  76. var builder = new SqlConnectionStringBuilder();
  77. builder.Password = protector.Unprotect(configuration["DbPassword"]);
  78. ...
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement