【问题标题】:Encrypting connectionStrings section - utility for app.config加密 connectionStrings 部分 - app.config 的实用程序
【发布时间】:2011-08-13 18:37:47
【问题描述】:

是否有一种实用程序可以加密app.config 文件中的命名配置部分(或只是connectionStrings 部分),其方式类似于可以将aspnet_regiisweb.config 文件一起使用?

我知道这可以在代码中完成 - 那里有代码示例,但我希望避免为此编写应用程序。

【问题讨论】:

  • Oded,想知道这样做的具体动机吗?
  • @wal - 加密所有连接字符串部分的紧急业务需求。使用aspnet_regiis 轻松处理web.config 文件,使用app.config 则不那么容易。
  • 如果它紧急/快速,那么我只能建议通过在文件 -> 高级属性下勾选“加密内容以保护数据”来加密整个文件。 :|
  • 在实践中也可能会更痛苦,具体取决于运行 IIS 的用户。不过,可能会让管理层感到高兴。

标签: .net encryption connection-string app-config


【解决方案1】:

您可以尝试以下方法:

https://magenic.com/thinking/encrypting-configuration-sections-in-net

简而言之 - 将 app.config 文件重命名为 web.config - 架构相同,因此 aspnet_regiis 有效。完成后重命名为app.config

【讨论】:

  • -1 - 这只是创建一个带有加密部分的web.config 文件,解密时为空。它甚至不看app.config 文件。
  • 这是我尝试的第一件事。没什么区别。在发布之前进行测试。
  • 对我来说似乎工作得很好,并且在几个环境中都有。从您的根 Web 文件夹运行此命令。您的 web.config 所在的位置。祝你好运
  • 我不明白的一件事是密钥是如何分配的?我的意思是客户端机器将不得不解密 configurationStrings 部分。它适用于我的开发人员机器,但我想 regiis 工具将密钥保存在某处。
【解决方案2】:

老问题,但这是微软的方式:

.NET 2.0: http://msdn.microsoft.com/en-us/library/89211k9b(v=vs.80).aspx

.NET 3.5: http://msdn.microsoft.com/en-us/library/ms254494(v=vs.90).aspx (“使用受保护的配置加密配置文件部分”部分)

在 app.config 文件上切换加密:

static void ToggleConfigEncryption(string exeConfigName)
{
    // Takes the executable file name without the 
    // .config extension. 
    try
    {
        // Open the configuration file and retrieve  
        // the connectionStrings section.
        Configuration config = ConfigurationManager.
            OpenExeConfiguration(exeConfigName);

        ConnectionStringsSection section =
            config.GetSection("connectionStrings")
            as ConnectionStringsSection;

        if (section.SectionInformation.IsProtected)
        {
            // Remove encryption.
            section.SectionInformation.UnprotectSection();
        }
        else
        {
            // Encrypt the section.
            section.SectionInformation.ProtectSection(
                "DataProtectionConfigurationProvider");
        }
        // Save the current configuration.
        config.Save();

        Console.WriteLine("Protected={0}",
            section.SectionInformation.IsProtected);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

【讨论】:

  • 你真的错过了问题的重点。这些链接根本没有解决问题。
  • 是不是你想要这个而根本不需要编写应用程序?
  • 是的。我正在寻找一个现有的实用程序。
  • 好的,知道了。我应该删除它吗?
  • 不必。它可能对将来访问该页面的人有用。
【解决方案3】:

编译这个控制台应用程序,并将一个配置文件拖到它上面。它会吐出一份配置文件的副本,其中的连接字符串已加密。

请注意,您必须以使用配置文件的同一用户身份进行加密。

using System;
using System.Configuration;
using System.IO;

namespace ConnectionStringEncryptor
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                throw new ArgumentException("Please supply a config file to encrypt");
            }
            string originalConfigFilePath = args[0];
            AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", originalConfigFilePath);
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            ConnectionStringsSection connectionStringsSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
            connectionStringsSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
            config.SaveAs(originalConfigFilePath + ".encrypted");
        }
    }
}

【讨论】:

  • 完美。不要忘记添加对 System.Configuration 的引用。
【解决方案4】:

基于 MichelZ 回答的 PowerShell 实现:

<#
.SYNOPSIS
Encrypts a section in .NET app configuration file.
#>
function Protect-DotNetConfigSection
{
    [CmdletBinding()]
    param
    (
        # Path to .exe file.
        [Parameter(Mandatory = $true)]
        [string] $ExePath,
        # List of section names.
        [Parameter(Mandatory = $true)]
        [string[]] $Sections
    )

    $config = [System.Configuration.ConfigurationManager]::OpenExeConfiguration($ExePath)

    foreach ($section in $Sections)
    {
        $config.GetSection($section).SectionInformation.ProtectSection('DataProtectionConfigurationProvider')
    }

    $config.Save()
}

Protect-DotNetConfigSection 'C:\MyApp\MyApp.exe' 'connectionStrings'
Protect-DotNetConfigSection 'C:\MyApp\MyApp.exe' @('connectionStrings', 'appSettings')

【讨论】:

  • 甜蜜又简单。绝对有资格作为 OP 的答案,因为将脚本复制并粘贴到文本文件中不被视为编写应用程序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-07
  • 1970-01-01
  • 2010-10-14
  • 2016-01-26
  • 1970-01-01
  • 2018-01-12
  • 1970-01-01
相关资源
最近更新 更多