I am using vs 2012(4.5) i use Encryption to Encrypt my ConnectionString,but when i call to
connStr = ConfigurationManager.ConnectionStrings["SnOracle"].ConnectionString;
i get this error object is not set to an instance of an object
I getting path to MyApp.exe.Config file that dont have ConnectionStringsSection
,but Encrypted MyApp.exe.Config.config DO have ConnectionStringsSection.Meybe i need to set path to
MyApp.exe.Config.config by my self or AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
need to return path to Encrypted file ?
How can i read ConnectionStringsSection from Encrypted MyApp.exe.Config.config file?
What i am doing wrong?
My code:
string path = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
// This return path to-> MyApp.exe.Config file that dont have ConnectionStringsSection
//But Encrypted MyApp.exe.Config.config DO have ConnectionStringsSection
Configuration config = ConfigurationManager.OpenExeConfiguration(path);
ConnectionStringsSection section = config.GetSection("connectionStrings") as ConnectionStringsSection;
if (!section.SectionInformation.IsProtected)
{
// Encrypt the section.
section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
}
step = "9";
// Remove encryption.
// section.SectionInformation.UnprotectSection();
// Save the current configuration.
config.Save();
step = "9a";
string connStr = "";
try
{
//here i getting the error
connStr = ConfigurationManager.ConnectionStrings["SnOracle"].ConnectionString;
}
catch (Exception ex)
{
//here afert it fails i insert it hard coded
connStr = "Data Source=xxx;User Id=xxx;Password=xxx";
}
MyApp.exe.Config:
<?xml version="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
<appSettings>
</appSettings>
</configuration>
MyApp.exe.Config.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings configProtectionProvider="DataProtectionConfigurationProvider">
<EncryptedData>
<CipherData>
<CipherValue>AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAffcYl/Mg5kKs24jULobCqAQAAAACAAAAAAADZgAAwAAAABAAAADYu948udrIvfvh5iP1Atz/AAAAAASAAACgfgLks14VfblBt3TX0E5ZnkwAAAASK+Jz3H9lx7DWVEQGXVS2HhqURpMbgFenk6v/lO6lMOk7Gv+tEhEmLs/zNApOaYNFAAAAJ1UfmT1bhg4zzVFv9/RcjNKjRzL</CipherValue>
</CipherData>
</EncryptedData>
</connectionStrings>
</configuration>