public static bool encryptConnexionString(out string msgErr) { msgErr = string.Empty; try { Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); ConfigurationSection cnxSection = config.GetSection(Constantes.sectionConnexionConfig); if (cnxSection != null) { //if (!cnxSection.SectionInformation.IsProtected) //Always encryter au k ou la chaine de connexion change... //{ if (!cnxSection.IsReadOnly() && !cnxSection.SectionInformation.IsProtected) { cnxSection.SectionInformation.ProtectSection(Constantes.ProviderCryptDecrypt); cnxSection.SectionInformation.ForceSave = true; config.Save(ConfigurationSaveMode.Full); //-------- return(true); } //} //else // msgErr="Cryptage déjà effectué"! } } catch (Exception ex) { msgErr = ex.Message; } return(false); }
public void SystemConfigurationSourceReturnsReadOnlySections() { SystemConfigurationSource source = new SystemConfigurationSource(false); ConfigurationSection dummySection = source.GetSection(localSection); Assert.IsTrue(dummySection.IsReadOnly()); }
// If a configuration section has a default value coming from 4.0 config and the AppDomain is opted in to auto-config upgrade, // set the configuration section element explicitly to its new 4.5 value. If the property has been explicitly set, no change // will be made. internal static void SetFX45DefaultValue(ConfigurationSection configSection, ConfigurationProperty property, object newDefaultValue) { if (BinaryCompatibility.Current.TargetsAtLeastFramework45 && !configSection.IsReadOnly()) { PropertyInformation propInfo = configSection.ElementInformation.Properties[property.Name]; Debug.Assert(propInfo != null); Debug.Assert(propInfo.Type.IsInstanceOfType(newDefaultValue)); if (propInfo.ValueOrigin == PropertyValueOrigin.Default) { try { propInfo.Value = newDefaultValue; } catch (ConfigurationErrorsException) { // Calling the Value setter might throw if the configuration element is locked. // We can technically override the "is locked?" check by calling the appropriate // method, but for now let's just honor the locks and ignore these errors. The // config sections we're touching shouldn't really ever be locked anyway, so // nobody should ever run into this in practice. } } } }
/// <summary> /// Encrypt the Config settings for the application /// </summary> /// <param name="config">The application config settings</param> public static void EncryptAppSettings(Configuration config) { try { ConfigurationSection section = config.GetSection("appSettings"); if (section != null) { if (!section.IsReadOnly() && !section.SectionInformation.IsProtected) { section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider"); section.SectionInformation.ForceSave = true; config.Save(ConfigurationSaveMode.Full); } } } catch (ConfigurationErrorsException ex) { log.ErrorFormat("Failed writing encrypted log {0}", ex); } catch (Exception ex) { log.ErrorFormat("Failed encrypting log {0}", ex); } }
private void ProtectSection(String sSectionName) { ConfigurationSection section = applicationConfiguration.GetSection(sSectionName); if (section != null) { if (!section.IsReadOnly()) { section.SectionInformation.ForceSave = true; applicationConfiguration.Save(ConfigurationSaveMode.Modified); } } }
private void EncryptConnString() { // Open the app.config file. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); // Get the section in the file. ConfigurationSection section = config.GetSection("connectionStrings"); // If the section exists and the section is not readonly, then protect the section. if (section != null) { if (!section.IsReadOnly()) { // Protect the section. section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider"); section.SectionInformation.ForceSave = true; // Save the change. config.Save(ConfigurationSaveMode.Modified); } } }
/// <summary> /// Encripta la información que aparece en los appsettings de la aplicacion /// </summary> static void EncryptConnectionStrings() { Configuration config = ConfigurationManager. OpenExeConfiguration (ConfigurationUserLevel.None); ConfigurationSection section = config.GetSection("connectionStrings"); if (section != null) { if (!section.IsReadOnly()) { if (!section.SectionInformation.IsProtected) { section.SectionInformation.ProtectSection ("DataProtectionConfigurationProvider"); section.SectionInformation. ForceSave = true; config.Save(ConfigurationSaveMode.Full); } } } }
protected void btnEncryptString_Click(object sender, EventArgs e) { string appPath = Request.ApplicationPath; Configuration configuration = WebConfigurationManager.OpenWebConfiguration(appPath); ConfigurationSection configurationSection = configuration.GetSection("connectionStrings"); if (configurationSection != null && !configurationSection.SectionInformation.IsLocked && !configurationSection.SectionInformation.IsProtected && !configurationSection.IsReadOnly()) { configurationSection.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider"); configurationSection.SectionInformation.ForceSave = true; configuration.Save(ConfigurationSaveMode.Full); } else { if (configurationSection != null) { configurationSection.SectionInformation.UnprotectSection(); configurationSection.SectionInformation.ForceSave = true; } configuration.Save(ConfigurationSaveMode.Full); } }