111,130
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
namespace MasterSoft.Common
{
public class ConfigrationHelper
{
/// Author:TerryLee
/// From:http://terrylee.cnblogs.com
/// </summary>
//加密配备文件
public static void EncryptConfiguration()
{
try
{
// 使用什么类型的加密
string provider = "RsaProtectedConfigurationProvider";
Configuration config = null;
config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// 加密连接字符串
ConfigurationSection section = config.ConnectionStrings;
ProtectSection(provider, config, section);
section = config.AppSettings;
ProtectSection(provider, config, section);
}
catch (Exception ex)
{
throw ex;
}
}
private static void ProtectSection(string provider, Configuration config, ConfigurationSection section)
{
try
{
if ((section.SectionInformation.IsProtected == false) &&
(section.ElementInformation.IsLocked == false))
{
section.SectionInformation.ProtectSection(provider);
section.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Full);
}
}
catch (Exception ex)
{
throw ex;
}
}
}
}