Encrypting connectionStrings section - utility for app.config

前端 未结 4 1975
不知归路
不知归路 2020-11-30 05:58

Is there a utility that will encrypt a named configuration section (or just the connectionStrings section) in an app.config file in a similar manne

相关标签:
4条回答
  • 2020-11-30 06:16

    Compile this console application, and drag a config file onto it. It will spit out a copy of the config file with its connection strings encrypted.

    Note that you must encrypt as the same user who will consume the config file.

    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");
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-30 06:20

    PowerShell implementation based on MichelZ's answer:

    <#
    .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')
    
    0 讨论(0)
  • 2020-11-30 06:35

    You can try the following:

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

    In short - rename the app.config file to web.config - the schema is identical, so aspnet_regiis works. Rename back to app.config when finished.

    0 讨论(0)
  • 2020-11-30 06:38

    Old question, but here is the Microsoft way:

    .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 (Section "Encrypting Configuration File Sections Using Protected Configuration")

    Toggle Encryption on app.config file:

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题