C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

 

Code Snippets

  

C# Source Code


Welcome to Dream.In.Code
Become a C# Expert!

Join 340,125 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 4,048 people online right now. Registration is fast and FREE... Join Now!




Secure the connectionStrings section of app.config file

This is a snippet I use to determine the encryption status of the connectionStrings section of the app.config file. If the section is already encrypted I remove the encryption so I can get the connection string, otherwise I encrypt it because I'm finished

Submitted By: PsychoCoder
Actions:
Rating:
Views: 5,656

Language: C#

Last Modified: November 8, 2008
Instructions: Add System.Configuration as a Reference then add using System.Configuration to the top of your class.

Pass the name of the application you wish the check (like "MyApplication.exe")

Snippet


  1. /// <summary>
  2. /// method for encrypting/decrypting the connectionString section of the
  3. /// app.config file
  4. /// </summary>
  5. /// <param name="appName">the name of the application
  6. ///</param>
  7. public bool CheckConnStringSectionStatus(string appName)
  8. {
  9.     try
  10.     {
  11.         // Open the configuration file and retrieve the connectionStrings section.
  12.         Configuration configFile = ConfigurationManager.OpenExeConfiguration(appName);
  13.  
  14.         //get the section we wish to work with (in this case the connectionStrings section
  15.         ConnectionStringsSection configSection = configFile.GetSection("connectionStrings") as ConnectionStringsSection;
  16.  
  17.         //check to see if the section is already encrypted
  18.         if (configSection.SectionInformation.IsProtected)
  19.         {
  20.             //it is so we need to remove the encryption
  21.             configSection.SectionInformation.UnprotectSection();
  22.         }
  23.         else
  24.         {
  25.             //not encrypted so we need to encrypt it
  26.             configSection.SectionInformation.ProtectSection("RSAProtectedConfigurationProvider");
  27.         }
  28.  
  29.         //re-save the configuration file
  30.         configFile.Save(ConfigurationSaveMode.Full, true);
  31.  
  32.         //return true since we were successful
  33.         return true;
  34.     }
  35.     catch (Exception ex)
  36.     {
  37.  
  38.         //return false since we failed
  39.         return false;
  40.     }
  41. }

Copy & Paste


Comments

There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month