2016-03-17 18 views
8

Với web.config biến mất, cách ưa thích để lưu trữ thông tin nhạy cảm (mật khẩu, mã thông báo) trong cấu hình của ứng dụng web được xây dựng bằng ASP.NET Core là gì?Cấu hình được mã hóa trong ASP.NET Core

Có cách nào để tự động nhận các phần cấu hình được mã hóa trong appsetttings.json không?

Trả lời

9

Bí mật của người dùng trông giống như một giải pháp tốt để lưu trữ mật khẩu và nói chung, bí mật ứng dụng, ít nhất là trong quá trình phát triển.

Kiểm tra điều này article hoặc this. Bạn cũng có thể kiểm tra this câu hỏi SO khác.

Đây chỉ là một cách để "ẩn" bí mật của bạn trong quá trình phát triển và tránh để tách chúng thành cây nguồn; Công cụ quản lý mật khẩu không mã hóa các bí mật được lưu trữ và không nên được coi là một cửa hàng đáng tin cậy.

Nếu bạn muốn đưa các ứng dụng được mã hóa.json vào sản xuất, không có giới hạn về điều đó. Bạn có thể xây dựng nhà cung cấp cấu hình tùy chỉnh của mình. Kiểm tra this.

Ví dụ:

public class CustomConfigProvider : ConfigurationProvider, IConfigurationSource 
    { 
     public CustomConfigProvider() 
     { 

     } 

     public override void Load() 
     { 
      Data = UnencryptMyConfiguration(); 
     } 

     private IDictionary<string, string> UnencryptMyConfiguration() 
     { 
      // do whatever you need to do here, for example load the file and unencrypt key by key 
      //Like: 
      var configValues = new Dictionary<string, string> 
      { 
       {"key1", "unencryptedValue1"}, 
       {"key2", "unencryptedValue2"} 
      }; 
      return configValues; 
     } 

     private IDictionary<string, string> CreateAndSaveDefaultValues(IDictionary<string, string> defaultDictionary) 
     { 
      var configValues = new Dictionary<string, string> 
      { 
       {"key1", "encryptedValue1"}, 
       {"key2", "encryptedValue2"} 
      }; 
      return configValues;     
     } 
     public IConfigurationProvider Build(IConfigurationBuilder builder) 
     { 
      return new CustomConfigProvider(); 
     } 
    } 

Định nghĩa một lớp tĩnh cho phương pháp mở rộng của bạn:

public static class CustomConfigProviderExtensions 
{    
     public static IConfigurationBuilder AddEncryptedProvider(this IConfigurationBuilder builder) 
     { 
      return builder.Add(new CustomConfigProvider()); 
     } 
} 

Và sau đó bạn có thể kích hoạt nó:

// Set up configuration sources. 
     var builder = new ConfigurationBuilder() 
      .AddJsonFile("appsettings.json") 
      .AddEncryptedProvider() 
      .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); 
+0

Tôi gặp vấn đề tương tự. bạn có thể hoàn thành UnencryptMyConfiguration dựa trên bình luận của bạn không? (ví dụ tải tập tin và khóa không mã hóa bằng khóa) –

+0

Tôi đã đăng một giải pháp dưới đây hoạt động cho tôi, đơn giản hơn rất nhiều và hoàn hảo cho nhu cầu của tôi. – CoderSteve

6

tôi không muốn viết một nhà cung cấp tùy chỉnh - cách làm việc quá nhiều. Tôi chỉ muốn gõ vào JsonConfigurationProvider, vì vậy tôi đã tìm ra cách làm việc cho tôi, hy vọng nó sẽ giúp một ai đó.

public class JsonConfigurationProvider2 : JsonConfigurationProvider 
{ 
    public JsonConfigurationProvider2(JsonConfigurationSource2 source) : base(source) 
    { 
    } 

    public override void Load(Stream stream) 
    { 
     // Let the base class do the heavy lifting. 
     base.Load(stream); 

     // Do decryption here, you can tap into the Data property like so: 

     Data["abc:password"] = MyEncryptionLibrary.Decrypt(Data["abc:password"]); 

     // But you have to make your own MyEncryptionLibrary, not included here 
    } 
} 

public class JsonConfigurationSource2 : JsonConfigurationSource 
{ 
    public override IConfigurationProvider Build(IConfigurationBuilder builder) 
    { 
     EnsureDefaults(builder); 
     return new JsonConfigurationProvider2(this); 
    } 
} 

public static class JsonConfigurationExtensions2 
{ 
    public static IConfigurationBuilder AddJsonFile2(this IConfigurationBuilder builder, string path, bool optional, 
     bool reloadOnChange) 
    { 
     if (builder == null) 
     { 
      throw new ArgumentNullException(nameof(builder)); 
     } 
     if (string.IsNullOrEmpty(path)) 
     { 
      throw new ArgumentException("File path must be a non-empty string."); 
     } 

     var source = new JsonConfigurationSource2 
     { 
      FileProvider = null, 
      Path = path, 
      Optional = optional, 
      ReloadOnChange = reloadOnChange 
     }; 

     source.ResolveFileProvider(); 
     builder.Add(source); 
     return builder; 
    } 
} 
+0

Chắc chắn đã giúp tôi. Cảm ơn rất nhiều. – ashilon

+0

Phương pháp mã hóa nào sẽ tốt hơn? – ssug89

+0

Đã giúp tôi. Cảm ơn! – blogs4t

Các vấn đề liên quan