ProtectDataConfigurationSource.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using EasyDevCore.Common.Security;
  7. using Microsoft.Extensions.Configuration;
  8. namespace EasyDevCore.Configuration
  9. {
  10. /// <summary>
  11. ///
  12. /// </summary>
  13. /// <seealso cref="Microsoft.Extensions.Configuration.IConfigurationSource" />
  14. public class ProtectDataConfigurationSource : IConfigurationSource
  15. {
  16. private readonly IConfigurationRoot _configurationRoot;
  17. /// <summary>
  18. /// The decrypt function
  19. /// </summary>
  20. private readonly Func<string, string, string> _decryptFunc;
  21. private readonly Func<string, string, string> _encryptFunc;
  22. /// <summary>
  23. /// Initializes a new instance of the <see cref="ProtectDataConfigurationSource" /> class.
  24. /// </summary>
  25. /// <param name="configurationRoot">The configuration root.</param>
  26. /// <param name="decryptFunc">The decrypt function.</param>
  27. /// <param name="encryptFunc">The encrypt function.</param>
  28. public ProtectDataConfigurationSource(IConfigurationRoot configurationRoot, Func<string, string, string> encryptFunc, Func<string, string, string> decryptFunc)
  29. {
  30. _configurationRoot = configurationRoot;
  31. _decryptFunc = decryptFunc;
  32. _encryptFunc = encryptFunc;
  33. }
  34. /// <summary>
  35. /// Builds the <see cref="T:Microsoft.Extensions.Configuration.IConfigurationProvider" /> for this source.
  36. /// </summary>
  37. /// <param name="builder">The <see cref="T:Microsoft.Extensions.Configuration.IConfigurationBuilder" />.</param>
  38. /// <returns>
  39. /// An <see cref="T:Microsoft.Extensions.Configuration.IConfigurationProvider" />
  40. /// </returns>
  41. public IConfigurationProvider Build(IConfigurationBuilder builder)
  42. {
  43. return new ProtectDataConfigurationProvider(_configurationRoot, _decryptFunc, _encryptFunc);
  44. }
  45. }
  46. }