Encrypting Connection Strings in app.config


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
 
Ad

Advertisements

M

Mr. Arnold

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top