现在的位置: 首页 > 综合 > 正文

用asp.net自带Provider给web.config加密的注意事项

2013年09月17日 ⁄ 综合 ⁄ 共 4086字 ⁄ 字号 评论关闭

 

下面是加密的程序,但是在本机加密以后把web.config传到服务器上以后会出现未能使用提供程序“RsaProtectedConfigurationProvider”进行解密 的错误信息。其实解决的办法很简单。只要把加密的程序一并传到服务器上。然后运行一下。这样就好用了(不过运行加密程序前,web.config应该是没加密的哦)。这个加密程序代码来自圣殿祭司的asp.net2.0开发详解

一:给connectionstrings加密

Configuration myConfiguration = null;
 ConfigurationSection myConnectionStrings = null;

    protected void Page_Load(object sender, EventArgs e)
    {

    }

 //以DPAPIProtectedConfigurationProvider加密
 protected void btnDPAIP_Click(object sender, EventArgs e)
 {
  try
  {
   getConnectionSettings(out myConfiguration, out myConnectionStrings);

   if (!myConnectionStrings.SectionInformation.IsProtected)
   {
    //DPAPI加密
    myConnectionStrings.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
    myConfiguration.Save(); //存储设置写入web.config文件
    new AlertMessage().showMsg(this.Page, "以DPAIP加密成功!");
   }
  }
  catch (Exception ex)
  {
   new AlertMessage().showMsg(this.Page,ex.Message.ToString());
  }
 }

 //以RSAProtectedConfigurationProvider加密
 protected void btnRSA_Click(object sender, EventArgs e)
 {
  try
  {
   getConnectionSettings(out myConfiguration, out myConnectionStrings);

   if (!myConnectionStrings.SectionInformation.IsProtected)
   {
    //RSA加密
    myConnectionStrings.SectionInformation.ProtectSection("RSAProtectedConfigurationProvider");
    myConfiguration.Save(); //存储设置写入web.config文件
    new AlertMessage().showMsg(this.Page, "以RSA加密成功!");
   }

  }
  catch (Exception ex)
  {
   new AlertMessage().showMsg(this.Page,ex.Message.ToString());
  }
 }

 //取得取得Web.config中connectionStrings设置区块
 protected void getConnectionSettings(out Configuration myConfig,out ConfigurationSection connStrings)
 {
  //打开Request所在路径网站的Web.config文件
  myConfig = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
  //取得Web.config中connectionStrings设置区块
  connStrings = myConfig.GetSection("connectionStrings");
 } 

 //解密
 protected void btnDecrypt_Click(object sender, EventArgs e)
 {
  try
  {
   getConnectionSettings(out myConfiguration, out myConnectionStrings);
   myConnectionStrings.SectionInformation.UnprotectSection(); //解密
   myConfiguration.Save();
   new AlertMessage().showMsg(this.Page, "connectionStrings解密成功!");
  }
  catch (Exception ex)
  {
   new AlertMessage().showMsg(this.Page,ex.Message.ToString());
  }
 }

 

二给appsettings加密

Configuration myConfiguration = null;
 ConfigurationSection myAppSettings = null;

    protected void Page_Load(object sender, EventArgs e)
    {

    }

 //以DPAPIProtectedConfigurationProvider加密
 protected void btnDPAIP_Click(object sender, EventArgs e)
 {
  try
  {
   getAppSettings(out myConfiguration, out myAppSettings);

   if (!myAppSettings.SectionInformation.IsProtected)
   {
    //DPAPI加密
    myAppSettings.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
    myConfiguration.Save(); //存储设置写入web.config文件
    new AlertMessage().showMsg(this.Page, "以DPAIP加密成功!");
   }
  }
  catch (Exception ex)
  {
   new AlertMessage().showMsg(this.Page,ex.Message.ToString());
  }
 }

 //以RSAProtectedConfigurationProvider加密
 protected void btnRSA_Click(object sender, EventArgs e)
 {
  try
  {
   getAppSettings(out myConfiguration, out myAppSettings);

   if (!myAppSettings.SectionInformation.IsProtected)
   {
    //RSA加密
    myAppSettings.SectionInformation.ProtectSection("RSAProtectedConfigurationProvider");
    myConfiguration.Save(); //存储设置写入web.config文件
    new AlertMessage().showMsg(this.Page, "以RSA加密成功!");
   }
  }
  catch (Exception ex)
  {
   new AlertMessage().showMsg(this.Page,ex.Message.ToString());
  }
 }

 //取得Web.config中appSettings设置区块
 protected void getAppSettings(out Configuration myConfig,out ConfigurationSection appSettings)
 {
  //开启Request所在路径网站的Web.config文件
  myConfig = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
  //取得Web.config中appSettings设置区块
  appSettings = myConfig.GetSection("appSettings");
 } 

 //解密
 protected void btnDecrypt_Click(object sender, EventArgs e)
 {
  try
  {
   getAppSettings(out myConfiguration, out myAppSettings);
   if (myAppSettings.SectionInformation.IsProtected)
   {
    myAppSettings.SectionInformation.UnprotectSection(); //解密
    myConfiguration.Save();
   }
   new AlertMessage().showMsg(this.Page, "appSettings解密成功!");
  }
  catch (Exception ex)
  {
   new AlertMessage().showMsg(this.Page,ex.Message.ToString());
  }
 }

 

 

抱歉!评论已关闭.