熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

加密和解密ASP.NET配置文件(Web.config)

2022-06-13   來源: .NET編程 

  首先添加引用: using SystemWebConfiguration;

加密操作如下:
PRivate void ProtectSection(string sectionName string provider)
    {
        Configuration config =
            WebConfigurationManager
                OpenWebConfiguration(RequestapplicationPath);

        ConfigurationSection section = configGetSection(sectionName);

        if (section != null && !sectionSectionInformationIsProtected)
        {
            sectionSectionInformationProtectSection(provider);
            configSave();
        }
    }

解密操作如下:
 private void UnProtectSection(string sectionName)
    {
        Configuration config =
            WebConfigurationManager
                OpenWebConfiguration(RequestApplicationPath);

        ConfigurationSection section = configGetSection(sectionName);

        if (section != null && sectionSectionInformationIsProtected)
        {
            sectionSectionInformationUnprotectSection();
            configSave();
        }
    }
實踐:
加密前的配置文件:
<?xml version=""?>
<configuration>
    <appSettings>
  <add key="name" value="shy" />
  <add key="address" value="cnblogs" />
 </appSettings>
    <systemweb>
        <compilation debug="true"/>
  </systemweb>
</configuration>

加密後的配置文件(Webconfig):
<?xml version=""?>
<configuration>
    <appSettings configProtectionProvider="DataProtectionConfigurationProvider">
  <EncryptedData>
   <CipherData>
    <CipherValue>
      AQAAANCMndBFdERjHoAwE/Cl+s
      BAAAABiATlNkEUGEfXyWGLXg
      QAAAACAAAAAAADZgAAqAAAABAAA
      ABIhxMWlazAntwIIpSTCDXAAAA
      AASAAACgAAAAEAAAAPz/YKYxc
      b+hfqdrfkLgAAAAXIeyc+WSx
      AfsDWvnC/fXsGTAnYeUgaCov
      enGFTHHsHgLiqKregFPYzGR
      vWxrez/VwOmJIeSEFKrcXej
      NnHLkgiNRkntLNZlZtTscZ
      wuVKAjsmiWsGBzGntL/
      UGHLELigrJcrYJ+lsjOscExQnv
      HGvAEfxpD+tEiFBtgXeHsFkQX
      cqGySshxvCtGUNUCZmEAhBh
      UsAFkPIYqelYHd+ma/xPetqw
      GIblawbWNDEfrzJPwnkfmpNqR
      hXijKImipwXbDVYyoUAAAAsD
      suYNOhJqAjJac/eUCsks=
      </CipherValue>
   </CipherData>
  </EncryptedData>
 </appSettings>
    <systemweb>
        <compilation debug="true"/>
  </systemweb>
</configuration>


From:http://tw.wingwit.com/Article/program/net/201311/14027.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.