博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

加密web.config中的敏感信息

Posted on 2007-04-14 14:12  james.dong  阅读(263)  评论(0编辑  收藏  举报

using System.Web.Configuration;
using System.Configuration;

public Class Config
{
       private void EncryptConfig( bool isEncrypt )
      {
            string path = "/webSite1";
            System.Configuration.Configuration  config   =WebConfigurationManager.OpenWebConfiguration ( path );      
            ConfigurationSection section = config.GetSection( "connectionstrings" );
            if( isEncrypt )
            {
                  section.SectionInformation.ProtectSection( "DataProtectionConfigurationProvider" );
            }
            else
            {
                  section.SectionInformation.UntProtectSection();
             }
            config.Save();
      }
}

web.config 文件:
<?xml version="1.0"?>
<configuration>
  
    <appSettings/>
    <connectionStrings>
        <add name="SimpleNorthwindsConnectionString1" connectionString="Data Source=deskzilla;Initial Catalog=SimpleNorthwinds;Persist Security Info=True;User ID=scott;Password=pw1337"
            providerName="System.Data.SqlClient" />
        <add name="TasksConnectionString1" connectionString="Data Source=JAMES-DONG;Initial Catalog=Tasks;User ID=sa;Pooling=False"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
    <system.web>
    </system.web>
</configuration>