Asp. NET database connection string encryption and decryption

Source: Internet
Author: User

in ASP. Net. config, the database connection string is encrypted and decrypted.

Although not very fresh, but believe that there are many people do not know, good, do not say nonsense, directly to the method: Start---> Run, enter cmd, and then enter the following content

Encryption:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe-pef "ConnectionStrings" "Your Web Project path"

Decrypt:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe-pdf "ConnectionStrings" "Your Web Project path"

. NET to modify the path of the version itself, where the name of the connectionstrings connection string.

Note that a native-based key is used in the encryption process, which means that the decryption process must be done on the same computer. If you move the encrypted Web. config file to another computer, the connection string in the Web. config file will not be decrypted properly.

The asp.net2.0 provides a simple encryption method that uses the Aspnet_iis.exe command, which is located in the following order:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727

Note that the system location and version number vary depending on the situation

Enter the directory in command mode to run, and the complete command is:

Aspnet_iis-pef "ConnectionStrings" "web.cofing Absolute Path (note: No need to enter Web. config)"

If "Success" is normally prompted, opening Web. config will find that the string is encrypted

The decryption command is:

Aspnet_iis-pdf "ConnectionStrings" "web.cofing Absolute Path (note: No need to enter Web. config)"

Note that encryption and decryption must be done on a single machine.

-----------------------------

The following shows the code to encrypt the database connection string, as follows:

Configuration config = webconfigurationmanager.openwebconfiguration (request.applicationpath);

ConfigurationSection configsection = config. GetSection ("connectionStrings");

if (configSection.SectionInformation.IsProtected)

... {//If it is already encrypted, no more encryption is necessary.

ConfigSection.SectionInformation.UnprotectSection ();

Config. Save ();

}

Else

... {

ConfigSection.SectionInformation.ProtectSection ("DataProtectionConfigurationProvider");

Config. Save ();

}

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.