Encrypting and decrypting asp.net configuration files (web.config)

Source: Internet
Author: User
Tags config

First add a reference: using System.Web.Configuration;




The
encryption operation is as follows:


PRivate void ProtectSection (String sectionname, string provider)


    {


Configuration config =


WebConfigurationManager.


openwebconfiguration (Request.applicationpath);





configurationsection section = config. GetSection (sectionname);





if (section!= null &&!section. sectioninformation.isprotected)


        {


section. Sectioninformation.protectsection (provider);


CONFIG. Save ();


        }


    }




The
decryption operation is as follows:


private void Unprotectsection (string sectionname)


    {


Configuration config =


WebConfigurationManager.


openwebconfiguration (Request.applicationpath);





ConfigurationSection section = Config. GetSection (sectionname);





if (section!= null && section. sectioninformation.isprotected)


        {


section. Sectioninformation.unprotectsection ();


CONFIG. Save ();


        }


    }


Practice:


configuration file before encryption:


<?xml version= "1.0"?>


<configuration>


<appSettings>


<add key= "name" value= "shy520"/>


<add key= "Address" value= "Cnblogs"/>


</appSettings>


<system.web>


<compilation debug= "true"/>


</system.web>


</configuration>





encrypted configuration file (web.config) :


<?xml version= "1.0"?>


<configuration>


<appsettings configprotectionprovider= "DataProtectionConfigurationProvider" >


<EncryptedData>


<CipherData>


<CipherValue>


Aqaaancmnd8bfderjhoawe/cl+s


Baaaabi1atlnkeugef0xywgl2xg


qaaaacaaaaaaadzgaaqaaaabaaa


abihxmwlazantwiipst1cdxaaaa


aasaaacgaaaaeaaaapz/ykyx07c


b+h4fqdr4fklgaaaax1ieyc+wsx


Afsdw1vn2c/fxsg2tanyeugacov


8e3ngfthhsh91gliqkregfpyzgr


Vw1xrez/3vwomji9es7efkrcxej


Nnhl66kg2inrk3ntlnzlztts3cz


9W63U47VKAJS6MIWSGBZ2GNTL/9


UGHLELIGRJCR3YJ+LSJOSCEXQNV


HGVA48EFXPD+TEIFBTGXEHSFKQX


CQGYSSHX16VCT2GUNUC3ZMEAHBH


USAFKPIYQELYHD4+M9A/XPE2TQW


GIBLA1WBW2NDEFRZJPWNKFMPNQR


hxijkimipwxbdvyy6o0uaaaas8d


suynohj7qajja2c/4euc7sks=


</CipherValue>


</CipherData>


</EncryptedData>


</appSettings>


<system.web>


<compilation debug= "true"/>


</system.web>


</configuration>

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.