我正在尝试加密控制台应用程序的App.config文件中的appSettings
和connectionStrings
部分。出于某种原因,section.SectionInformation.IsProtected
总是返回true。
static void Main(string[] args)
{
EncryptSection("connectionStrings", "DataProtectionConfigurationProvider");
}
private static void EncryptSection(string sectionName, string providerName)
{
string assemblyPath = Assembly.GetExecutingAssembly().Location;
Configuration config = ConfigurationManager.OpenExeConfiguration(assemblyPath);
ConfigurationSection section = config.GetSection(sectionName);
if (section != null && !section.SectionInformation.IsProtected)
{
section.SectionInformation.ProtectSection(providerName);
config.Save();
}
}
不确定为什么它总是返回true。
答案 0 :(得分:2)
您的代码会打开当前的应用程序配置。你可以试试这个:
static void Main(string[] args)
{
if (args.Length != 0)
{
Console.Error.WriteLine("Usage : Program.exe <configFileName>"); // App.Config
}
EncryptSection(args[0], "connectionStrings", "DataProtectionConfigurationProvider");
}
private static void EncryptSection(string configurationFile, string sectionName, string providerName)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(configurationFile);
ConfigurationSection section = config.GetSection(sectionName);
if (section != null && !section.SectionInformation.IsProtected)
{
section.SectionInformation.ProtectSection(providerName);
config.Save();
}
}