ConnectionStrings common methods in cryptographic configuration files (app. Config and web. config)

Source: Internet
Author: User

1, background : According to the requirements of the project, you need to encrypt the configuration file configuration database connection string, that is, the content of the connectinstring node is encrypted storage, taking into account that the code uses the connection string does not need to make changes, C # The encrypted content is automatically decrypted.

2, requirements : So consider developing a separate WinForm program, to encrypt and decrypt the configuration file, Creates a configuration object from the Openmappedexeconfiguration method in ConfigurationManager and then operates on the specified node in the object.

Problems encountered:

Configurationmanager.openexeconfiguration ("C:\Charles2008.config");

This method produces a copy ("C:\Charles2008.config.config") in the current directory, And the returned configuration object operation is not the Charles2008.config file, but the program automatically creates the Charles2008.config.config file, however if I rename the file Charles2008.config to CHARLES20 08.config.config file or delete charles2008.config file but prompt:

Error loading configuration file: Parameter ' ExePath ' is invalid.
Parameter name: ExePath

3, the solution : From the Internet to find help, found that there are really and I encountered the same problem, just need to make a little change in the above code, the changes do not generate a copy of the file, directly manipulate the file, update is also the operation of this file.

//first, instantiate a Execonfigurationfilemap object and assign the physical address to its execonfigfilename attribute;
Execonfigurationfilemap Filemap =NewExeconfigurationfilemap ();
Filemap.execonfigfilename =@"C:\Charles2008.config";

//then call Filemap to instantiate config, so that the action file is the Charles2008.config file and does not produce a copy file
Configuration config = configurationmanager.openmappedexeconfiguration (filemap, Configurationuserlevel.none);

4. Encrypt the connection string : The above technical difficulty is resolved, the connection string can be encrypted according to the following methods obtained from the Configuration object config. (for conectionstring nodes only)

ConfigurationSection connectionsection = config. GetSection ("connectionStrings");
if(Connectionsection! =NULL)
{
ConnectionSection.SectionInformation.ProtectSection ("RsaProtectedConfigurationProvider");
Config. Save ();
MessageBox.Show ("Save success!","Tips", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

5. Remark :

Encryption and decryption are used on the same computer, and a native-based key is used during the encryption process. This means that encryption and decryption must be done on the same computer, or it will not be decrypted. Also, the encrypted configuration on one computer will not work correctly on another computer.

Connection string Encryption Tool

ConnectionStrings common methods in cryptographic configuration files (app. Config and web. config)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.