簡體   English   中英

在App.config文件控制台應用程序中保護部分

[英]Protecting Section in App.config file Console Application

我正在嘗試加密控制台應用程序的App.config文件中的appSettingsconnectionStrings部分。 由於某種原因, 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(); 
    }
}

不知道為什么它總是回歸真實。

您的代碼打開當前的應用程序配置。 你可以試試這個:

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();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM