Encrypting Connection Strings in app.config

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm trying to encrypt a connection string in an app.config.

This is the app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="test" connectionString="user
id=testuser;password=testpwd;data source=testdb;persist security
info=False;"/>
</connectionStrings>
</configuration>

Here's the code:

private void btnEncrypt_Click(object sender, EventArgs e)
{
Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

ConnectionStringsSection section =
(ConnectionStringsSection)
config.GetSection("connectionStrings");

if (!section.SectionInformation.IsProtected)

section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");

config.Save(ConfigurationSaveMode.Full);

txtDisplay.Text = "Encrypted";

}

I have
using System.Configuration;
at the top and a reference to System.Configuration in the references.

When I run it, it displays the "encrypted" message. There are no errors and
it completes, yet the app,.config section is not encrypted.

Any help is appreciated.

Thanks,

Mike
 
Mike Robbins said:
I'm trying to encrypt a connection string in an app.config.

I have
using System.Configuration;
at the top and a reference to System.Configuration in the references.

When I run it, it displays the "encrypted" message. There are no errors
and
it completes, yet the app,.config section is not encrypted.

Are you sure you're looking at the right config file?

There is the app.config file, and then there is the appnamerun.config file
that's located at the location of the exe that holds the app.config data at
runtime, which ConfigurationManager can manipulate.
 
Back
Top