效用是否有一個實用工具,將類似的方式,人們可以使用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