Encrypting connectionStrings section - utility for app.config

前端 未结 4 1974
不知归路
不知归路 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: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')
    

提交回复
热议问题