Skip to main content
 首页 » 编程设计

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

2024年01月13日28xxx_UU

我看到可以使用 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