web_config加密解密2

下面是加密的程序,但是在本机加密以后把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());
  }
 }


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/mubingyun/archive/2008/12/09/3483772.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值