Asp.net web. config encryption and decryption

Source: Internet
Author: User

From: cnblogs font size: [large, medium, small]
  • Abstract: This article introduces the encryption and decryption of Asp.net web. config, and provides simple sample code for your reference.
The Web. config file is an XML text file used to store the configuration information of Web applications in ASP. NET. Web application developers usually configure some key information of the application in the web. config file, such as the database connection string. However, the Web. config file also has certain security risks. Such key information stored in the plaintext mode may become a breakthrough for others, resulting in unnecessary losses. Currently, the best way to solve this problem is to encrypt the string of the web. config file and decrypt it when you want to modify the information. Next I will introduce a common web. config encryption and decryption method.
Implementation process.
(1) first, we add two buttons to the page and write the encryption code in the Click Event of the encryption button. (Here we need to reference using system. Web. configuration; namespace)

// Open the configuration file configuration Config = webconfigurationmanager in the application. openwebconfiguration (request. applicationpath); // find the specified configurationsection = config in the opened configuration file. getsection ("deleetask"); If (section! = NULL &&! Section. sectioninformation. isprotected) {Section. sectioninformation. protectsection ("rsaprotectedconfigurationprovider"); config. Save (); response. Write ("<SCRIPT> alert ('encrypted! '); </SCRIPT> ");}

(2) write the decryption code in the Click Event of the decryption button.

Configuration Config = webconfigurationmanager. openwebconfiguration (request. applicationpath); configurationsection section = config. getsection ("etettings"); If (section! = NULL & section. sectioninformation. isprotected) {Section. sectioninformation. unprotectsection (); config. save (); response. write ("<SCRIPT> alert ('decrypted successfully'); </SCRIPT> ");}

As follows:

Before encryption:

After encryption:

After decryption:

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.