效用是否有一个实用工具,将类似的方式,人们可以使用aspnet_regiis
与web.config
加密文件在app.config
文件中的命名配置部分(或只是connectionStrings
节)?加密connectionStrings节 - 为的app.config
我知道这可以在代码中完成 - 这里有代码示例,但我希望避免为此编写应用程序。
效用是否有一个实用工具,将类似的方式,人们可以使用aspnet_regiis
与web.config
加密文件在app.config
文件中的命名配置部分(或只是connectionStrings
节)?加密connectionStrings节 - 为的app.config
我知道这可以在代码中完成 - 这里有代码示例,但我希望避免为此编写应用程序。
你可以尝试以下方法:
总之 - 的app.config
文件重命名为web.config
- 该架构是相同的,因此aspnet_regiis
作品。完成后重命名为app.config
。
老问题,但在这里是微软的方法:
.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);
}
}
编译这个控制台应用程序,并将一个配置文件拖到它上面。它会将其连接字符串加密的配置文件的副本吐出。
请注意,您必须以使用配置文件的相同用户进行加密。
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的引用。 – mhenry1384 2016-07-27 15:20:03
Oded,好奇地知道这个具体的动机? – wal 2011-04-27 11:59:25
@wal - 加急所有连接字符串部分的紧急业务要求。使用'aspnet_regiis'很容易在'web.config'文件上完成,而'app.config'则不那么容易。 – Oded 2011-04-27 12:14:32
如果它的紧急/快速,那么我只能建议通过勾选文件 - >属性高级下的'加密内容以保护数据'来加密整个文件。 :| – wal 2011-04-27 12:50:35