转载:加密配置文件 和 解密配置文件
/// <summary>
/// 加密配置文件
/// </summary>
/// <param name="sectionName">要加密的 节("appSettings"或"connectionStrings")</param>
/// <param name="provider">加密的程序名("DataProtectionConfigurationProvider")</param>
private void ProtectSection(string sectionName, string provider)
{
Configuration config =
System.Web.Configuration.WebConfigurationManager.
OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection section = config.GetSection(sectionName);
if (section != null && !section.SectionInformation.IsProtected)
{
section.SectionInformation.ProtectSection(provider);
config.Save();
}
}
/// <summary>
/// 解密配置文件
/// </summary>
/// <param name="sectionName">要解密的 节 ("appSettings"或"connectionStrings")</param>
private void UnProtectSection(string sectionName)
{
Configuration config =
System.Web.Configuration.WebConfigurationManager.
OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection section = config.GetSection(sectionName);
if (section != null && section.SectionInformation.IsProtected)
{
section.SectionInformation.UnprotectSection();
config.Save();
}
}
WebConfig文件中包含了很多重要的配置信息,部署的时候我们需要对其进行加密.
加密方法:
在命令行中输入,
aspnet_regiis.exe -pef "connectionStrings" "D:\Project\Asp.netTest\"
(aspnet_regiis.exe在C:\WINDOWS\Microsoft.Net\Framework\v2.0.50727文件夹里)
-pef 对指定物理(非虚拟)目录中的 Web.config 文件的指定配置节进行加密。
对应的这个解密则是
-pdf 参数 对指定物理(非虚拟)目录中的 Web.config 文件的指定配置节进行解密。
connectionStrins是要加密的部分,后面的是工程的路径.
获取
<connectionStrings>
<add name="CJGLConnectionString" c
providerName="System.Data.SqlClient" />
</connectionStrings>
中的连接自符串:
System.Configuration.ConnectionStringSettings s = ConfigurationManager.ConnectionStrings["CJGLConnectionString"];
this.Label1 .Text = s.ConnectionString ;
还要添加对System.Configuration.dll 文件的引用
1.写入连接字符串
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// Create a new ConnectionStringSettings object and populate it
ConnectionStringSettings conn = new ConnectionStringSettings();
conn.ConnectionString =
"Server=localhost;User ID=sa;Password=123456; " +
"Database=Northwind;Persist Security Info=True";
conn.Name = "AppConnectionString2";
conn.ProviderName = "System.Data.SqlClient";
// Add the new connection string to the web.config
Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("example");
config.ConnectionStrings.ConnectionStrings.Add(conn);
config.Save();
}
}
2.修改连接字符串
protected void Page_Load(object sender, EventArgs e)
{
// Retrieve an existing connection string into a Connection String Builder
System.Data.SqlClient.SqlConnectionStringBuilder builder = new
System.Data.SqlClient.SqlConnectionStringBuilder();
// Change the connection string properties
builder.DataSource = "localhost";
builder.InitialCatalog = "Northwind1";
builder.UserID = "sa";
builder.Password = "password";
builder.PersistSecurityInfo = true;
// Save the connection string back to the web.config
Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/Chapter11");
config.ConnectionStrings.ConnectionStrings["AppConnectionString1"].ConnectionString =
builder.ConnectionString;
config.Save();
}
获取
<appSettings>
<add key="ConnectionString" value="Data Source=218.98.101.165;Initial Catalog=HRMS;User ID=hrms;Password=123456"/>
</appSettings>
中的连接自符串:
string str = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];