I'm trying to encrypt my connection string , which is in my app.config
.
After reading some forums, I saw that 2 methods need to be created:
class proteger_app
{
public static void Criptografar()
{
Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationSection section =
config.ConnectionStrings;
if (!section.SectionInformation.IsProtected)
{
section.SectionInformation.ProtectSection("RSAProtectedConfigurationProvider");
section.SectionInformation.ForceSave = true;
config.Save();
}
}
public static void Decriptografar()
{
Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConnectionStringsSection section =
config.ConnectionStrings;
if (section.SectionInformation.IsProtected)
{
section.SectionInformation.UnprotectSection();
section.SectionInformation.ForceSave = true;
config.Save();
}
}
}
So far beauty, methods created, and then?
public void chek()
{
string sqconn, _sql;
int th;
proteger_app.Decriptografar();
sqconn = ConfigurationManager.ConnectionStrings["sql brayton"].ConnectionString;
proteger_app.Criptografar();
_sql = @"SELECT id FROM base64";
SqlConnection con = new SqlConnection(sqconn);
try
{
But when I look at app.config
is unencrypted.