The connectionStrings common method in the encrypted configuration file (App. Config and Web. config), connectionstrings

Source: Internet
Author: User

The connectionStrings common method in the encrypted configuration file (App. Config and Web. config), connectionstrings

1,Background: According to the project requirements, you need to encrypt the database connection string configured in the configuration file, that is, to encrypt and store the content of the ConnectinString node, and consider that the Code does not need to be changed when using the connection string, C # automatically decrypts the encrypted content.

2,Requirement: Therefore, you need to develop a winform program to encrypt and decrypt the Configuration file, create a Configuration object through the OpenMappedExeConfiguration method in ConfigurationManager, and then operate on the specified node in the object.

Problems:

ConfigurationManager. OpenExeConfiguration ("C: \ Charles2008.config ");

This method generates a copy ("C: \ Charles2008.config. config "), and the returned Configuration object is not the Charles2008.config file, but the Charles2008.config automatically created by the program. config file. However, if I change the name of Charles2008.config to Charles2008.config. the following message is displayed when you delete the config file or the Charles2008.config file:

 

An error occurred while loading the configuration file: The parameter "exePath" is invalid.
Parameter Name: exePath

3,Solution: Looking for help from the Internet, I found that I still encountered the same problem. I only need to make a slight change to the above Code. After the change, I will not generate a copy of the file and directly operate the file, update also operates on this file.

// Instantiate an ExeConfigurationFileMap object and assign the physical address to its ExeConfigFilename attribute;
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap ();
FileMap. ExeConfigFilename = @ "C: \ Charles2008.config ";

// Call fileMap to instantiate config. In this way, the operated file is the Charles2008.config file, and no copy file will be generated.
Configuration config = ConfigurationManager. OpenMappedExeConfiguration (fileMap, ConfigurationUserLevel. None );

4,Encrypted connection string: After the above technical difficulties are solved, you can encrypt the connection string by calling the following method based on the Configuration object Config. (Only for ConectionString nodes)

ConfigurationSection connectionSection = config. GetSection ("connectionStrings ");
If (connectionSection! = Null)
{
ConnectionSection. SectionInformation. ProtectSection ("RSAProtectedConfigurationProvider ");
Config. Save ();
MessageBox. Show ("saved successfully! "," Prompt ", MessageBoxButtons. OK, MessageBoxIcon. Information );
}

5,Remarks:

Encryption and decryption are used on the same computer. During encryption, a local key is used. This means that encryption and decryption must be performed on the same computer; otherwise, encryption cannot be performed. At the same time, the encrypted configuration on one computer will not work properly on the other computer.

Connect to the string encryption tool

 

Related Article

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.