Protecting Section in App.config file Console Application

Question!

I am trying to encrypt the appSettings and connectionStrings section in App.config file of the console application. For some reason section.SectionInformation.IsProtected is always returning 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(); 
    }
}

Not sure why it is always returning true.

By : azamsharp


Answers

Your code opens the current application configuration. You can try this :

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


I just went through the same thing. Ended up switching and making a short little JSF app (running on Glassfish) that uses JasperReports to print directly to the lable printer. Push button, instant label at the printer, don't even have to view it on-screen if you don't want to since Jasper can directly output to printer (as well as PDF in browser).



I also struggled with the HTML/CSS approach due to the inconsistent printing behaviour across browsers.

I created a C# library to produce Avery Labels from ASP.NET which I hope you might find useful:

https://github.com/wheelibin/SharpPDFLabel#readme

You can add images and text to the labels, and it's easy to define more labels types.

(I use it for barcode labels, the barcode is generated as an image and then added to the label using this library.)

Cheers

By : wheelibin


This video can help you solving your question :)
By: admin