c# - 在单声道中加密 web.config 的配置部分

标签 c# .net encryption mono

我看到可以使用 ASP_regiis 来加密 web.config 文件的部分,但我在使用 Apache 的机器上运行单声道。有没有办法在 Mono/Linux 中做到这一点?

最佳答案

您可以使用 System.Configuration.ConfigurationManager 以编程方式执行此操作以获取 ConfigurationSection 对象并对其调用 SectionInformation.ProtecteSection("DataProtectionConfigurationProvider")

    /// <summary>
    /// Encrypts a Config section from the given Configuration object
    /// </summary>
    /// <param name="sectionKey">Path to the section to Encrypt</param>
    /// <param name="config">Configuration</param>
    public static void EncryptConfigSection(String sectionKey, Configuration config)
    {
        ConfigurationSection section = config.GetSection(sectionKey);
        if (section != null)
        {
            if (!section.SectionInformation.IsProtected)
            {
                if (!section.ElementInformation.IsLocked)
                {
                    section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
                    section.SectionInformation.ForceSave = true;
                    config.Save(ConfigurationSaveMode.Full);
                }
            }
        }
    }

对于 Web 配置,您需要使用 System.Web.Configuration.WebConfigurationManager 来获取配置对象,然后您可以将该对象传递给上述函数。请注意,对于 web.config 文件,只有某些部分是可加密的。

另请注意,如果设置存储在 AppSettings 中,那么任何人都可以编写一个简单的应用程序,该应用程序在您的服务器上运行时可以检索设置的纯文本值,前提是他们知道您的设置名称。

查看 Jon Galloway 撰写的以下文章,了解简单加密 AppSettings 部分的替代方法:http://weblogs.asp.net/jgalloway/archive/2008/04/13/encrypting-passwords-in-a-net-app-config-file.aspx

关于c# - 在单声道中加密 web.config 的配置部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/639125/

相关文章:

c# - 设计时用户控件中的集合编辑器

c# - NSec.Cryptography 使用 ChaCha20Poly1305 和 SharedSecret 进行加密和解密

javascript - 字符串加密 - 生成独特的模式,如 Spotify 代码

c# - MVC LINQ动态排序通过获取列类型

c# - services.AddSingleton<IConfiguration> 在 .NET Core 中的用途

.net - "Overflow"编译器错误 -9223372036854775808L

c++ - 在 C++ 生成器中使用 hashlib++ 时出错?

c# - 如何强制在 chrome android 上内联打开 pdf 文档?

c# - Autofixture Fixture.Customize - 为什么重载有不同的返回类型?

.net - 在 .net 中获取系统特定的换行符