Encrypting connectionStrings section - utility for app.config

前端 未结 4 1980
不知归路
不知归路 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");
            }
        }
    }
    

提交回复
热议问题