webconfig中对区块的加密

本文介绍如何在ASP.NET应用中对配置文件的appSettings区块进行加密和解密,包括使用DPAPI和RSA加密方式,并提供了解密方法及数据库连接字符串的加密技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ContractedBlock.gifExpandedBlockStart.gifCode
  protected void Page_Load(object sender, EventArgs e)
    {
        Configuration config 
= WebConfigurationManager.OpenWebConfiguration(this.Request.ApplicationPath);
        MailSettingsSectionGroup smtp 
= (MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");

        Response.Write(smtp.Smtp.Network.Host);
    }
    
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    
protected void Button1_Click(object sender, EventArgs e)
    {

        
try
        {
            getAppSettings(
out myConfiguration, out myAppSettings);
            
if (!myAppSettings.SectionInformation.IsProtected)
            {
                myAppSettings.SectionInformation.ProtectSection(
"DataProtectionConfigurationProvider");
                myConfiguration.Save();
                Response.Write(
"success jiami");
            }
        }
        
catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
    
protected void getAppSettings(out Configuration myconfig, out ConfigurationSection appSettings)
    {
        myconfig 
= WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
        appSettings 
= myconfig.GetSection("appSettings");
    }

    
protected void Button3_Click(object sender, EventArgs e)
    {
        
try
        {
            getAppSettings(
out myConfiguration, out myAppSettings);
            
if (myAppSettings.SectionInformation.IsProtected)
            {
                myAppSettings.SectionInformation.UnprotectSection();
                myConfiguration.Save();
            }
            Response.Write(
"success jiemim");
        }

        
catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    
    }
}

以上就可以对appsettings的区块内容加密了,使用"DataProtectionConfigurationProvider"进行DPAPI加密

用"RSAProtectionConfigurationProvider"就可以进行RSA加密了,只要改改provider就可以了。

用简单的UnprotectSection()就可以解密了

如果对数据库连接字符串部分加密可以用

myConfig=GetSection("connectionStrings");

就可以了

 

用aspnet_regiis -pe(加密,解密用-pd) “connectionStrings" -app "虚拟目录,在c:\intpub\wwwroot中" -prov "加密方式"

转载于:https://www.cnblogs.com/newos/archive/2009/04/25/1443379.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值