What's Here?
- Members: 306,845
- Replies: 841,328
- Topics: 140,571
- Snippets: 4,465
- Tutorials: 1,166
- Total Online: 1,699
- Members: 122
- Guests: 1,577
|
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
|
|
Rating:
   
|
|
Views: 4,911 |
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
/// <summary>
/// method for encrypting/decrypting the connectionString section of the
/// app.config file
/// </summary>
/// <param name="appName">the name of the application
///</param>
public bool CheckConnStringSectionStatus(string appName)
{
try
{
// Open the configuration file and retrieve the connectionStrings section.
Configuration configFile = ConfigurationManager.OpenExeConfiguration(appName);
//get the section we wish to work with (in this case the connectionStrings section
ConnectionStringsSection configSection = configFile.GetSection("connectionStrings") as ConnectionStringsSection;
//check to see if the section is already encrypted
if (configSection.SectionInformation.IsProtected)
{
//it is so we need to remove the encryption
configSection.SectionInformation.UnprotectSection();
}
else
{
//not encrypted so we need to encrypt it
configSection.SectionInformation.ProtectSection("RSAProtectedConfigurationProvider");
}
//re-save the configuration file
configFile.Save(ConfigurationSaveMode.Full, true);
//return true since we were successful
return true;
}
catch (Exception ex)
{
//return false since we failed
return false;
}
}
Copy & Paste
|
|
|
|