ASP.NET2.0中設定檔的加密與解密

來源:互聯網
上載者:User
asp.net|加密|解密 首先添加引用: using System.Web.Configuration;

加密操作如下:
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();
        }
    }

解密操作如下:
 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();
        }
    }
實踐:
加密前的設定檔:
<?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>
加密後的設定檔:
<?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>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.