【问题标题】:Is it possible to protect a single element in the appSettings section instead of the entire section?是否可以保护 appSettings 部分中的单个元素而不是整个部分?
【发布时间】:2012-09-24 20:30:37
【问题描述】:

我想保护我的 appSettings 中的一个键/值对,而不是使用我之前使用 ProtectSection 方法完成的其他键/值对,如下所示。

var configurationSection = config.GetSection("appSettings");
configurationSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");

理想情况下,我想做如下的事情:

var configurationElement = config.GetSection("appSettings").GetElement("Protected");
configurationElement.ElementInformation.ProtectElement("DataProtectionConfigurationProvider");

这是我将要操作的示例 appSettings:

<configuration>
<appSettings>
    <add key="Unprotected" value="ChangeMeFreely" />
    <add key="Protected" value="########"/>
</appSettings>
</configuration>

我一直在寻找,但还没有找到方法。这可能吗?

【问题讨论】:

    标签: c# .net app-config


    【解决方案1】:

    并非开箱即用 - .NET 为您提供了加密部分的能力 - 但不是单个元素。但是,由于这些只是字符串,因此您绝对可以自己创建一些方案来在将字符串保存到文件之前对其进行加密,并在从配置文件中读取它之后对其进行解密。

    但这不会是透明的 - 你必须自己做,你必须明确地做。

    【讨论】:

      【解决方案2】:

      当我需要从应用设置部分加密单个值时,我遇到了同样的问题。我使用了DpapiProtectedConfigurationProvider 类的EncryptTextDecryptText 私有方法,它们允许我加密任何文本值,而不必加密配置元素。

      这里是辅助类:

      public class WebConfigEncryption
      {
          private readonly DpapiProtectedConfigurationProvider _provider;
          private readonly MethodInfo _encryptTextMethod;
          private readonly MethodInfo _decryptTextMethod;
      
          public WebConfigEncryption()
          {
              _provider = new DpapiProtectedConfigurationProvider();
              _encryptTextMethod = _provider.GetType().GetMethod("EncryptText", BindingFlags.Instance | BindingFlags.NonPublic);
              _decryptTextMethod = _provider.GetType().GetMethod("DecryptText", BindingFlags.Instance | BindingFlags.NonPublic);
          }
      
          public string Encrypt(string value)
          {
              var encryptedValue = value != null ? (string)_encryptTextMethod.Invoke(_provider, new object[] { value }) : null;
      
              return encryptedValue;
          }
      
          public string Decrypt(string value)
          {
              var decryptedValue = value != null ? (string)_decryptTextMethod.Invoke(_provider, new object[] { value }) : null;
      
              return decryptedValue;
          }
      }
      

      使用示例:

      [Test]
      public void EncryptDecryptTest()
      {
          var instance = new WebConfigEncryption();
      
          var encrypted = instance.Encrypt("123");
          var decrypted = instance.Decrypt(encrypted);
          Assert.That(decrypted, Is.EqualTo("123"));
      }
      

      此外,如果您有权访问 XmlNodeXmlElement 实例,则可以使用提供程序类的公共方法:DpapiProtectedConfigurationProvider.Encrypt(XmlNode)DpapiProtectedConfigurationProvider.Decrypt(XmlNode) 而不是反射。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-17
        • 2012-07-25
        • 1970-01-01
        • 2019-04-28
        • 2023-03-15
        • 1970-01-01
        • 1970-01-01
        • 2016-02-18
        相关资源
        最近更新 更多