<PackageReference Include="System.Configuration.ConfigurationManager" Version="8.0.0-preview.7.23375.6" />

ProtectedConfigurationSection

Provides programmatic access to the configProtectedData configuration section. This class cannot be inherited.
using System.Collections.Specialized; using System.Globalization; using System.Xml; namespace System.Configuration { public sealed class ProtectedConfigurationSection : ConfigurationSection { private const string EncryptedSectionTemplate = "<{0} {1}=\"{2}\"> {3} </{0}>"; private static readonly ConfigurationProperty s_propProviders = new ConfigurationProperty("providers", typeof(ProtectedProviderSettings), new ProtectedProviderSettings(), ConfigurationPropertyOptions.None); private static readonly ConfigurationProperty s_propDefaultProvider = new ConfigurationProperty("defaultProvider", typeof(string), "RsaProtectedConfigurationProvider", null, ConfigurationProperty.s_nonEmptyStringValidator, ConfigurationPropertyOptions.None); private static readonly ConfigurationPropertyCollection s_properties = new ConfigurationPropertyCollection { s_propProviders, s_propDefaultProvider }; protected internal override ConfigurationPropertyCollection Properties => s_properties; private ProtectedProviderSettings ProtectedProviders => (ProtectedProviderSettings)base[s_propProviders]; [ConfigurationProperty("providers")] public ProviderSettingsCollection Providers { get { return ProtectedProviders.Providers; } } [ConfigurationProperty("defaultProvider", DefaultValue = "RsaProtectedConfigurationProvider")] public string DefaultProvider { get { return (string)base[s_propDefaultProvider]; } set { base[s_propDefaultProvider] = value; } } internal ProtectedConfigurationProvider GetProviderFromName(string providerName) { ProviderSettings providerSettings = Providers[providerName]; if (providerSettings == null) throw new ArgumentException(System.SR.Format(System.SR.ProtectedConfigurationProvider_not_found, providerName), "providerName"); return InstantiateProvider(providerSettings); } internal ProtectedConfigurationProviderCollection GetAllProviders() { ProtectedConfigurationProviderCollection protectedConfigurationProviderCollection = new ProtectedConfigurationProviderCollection(); foreach (ProviderSettings provider in Providers) { protectedConfigurationProviderCollection.Add(InstantiateProvider(provider)); } return protectedConfigurationProviderCollection; } private static ProtectedConfigurationProvider CreateAndInitializeProviderWithAssert(Type t, ProviderSettings pn) { ProtectedConfigurationProvider protectedConfigurationProvider = (ProtectedConfigurationProvider)TypeUtil.CreateInstance(t); NameValueCollection parameters = pn.Parameters; NameValueCollection nameValueCollection = new NameValueCollection(parameters.Count); foreach (string item in parameters) { nameValueCollection[item] = parameters[item]; } protectedConfigurationProvider.Initialize(pn.Name, nameValueCollection); return protectedConfigurationProvider; } private static ProtectedConfigurationProvider InstantiateProvider(ProviderSettings pn) { Type type = TypeUtil.GetType(pn.Type, true); if (!typeof(ProtectedConfigurationProvider).IsAssignableFrom(type)) throw new ArgumentException(System.SR.WrongType_of_Protected_provider, "pn"); return CreateAndInitializeProviderWithAssert(type, pn); } internal static string DecryptSection(string encryptedXml, ProtectedConfigurationProvider provider) { XmlDocument xmlDocument = new XmlDocument(); xmlDocument.LoadXml(encryptedXml); XmlNode xmlNode = provider.Decrypt(xmlDocument.DocumentElement); return xmlNode.OuterXml; } internal static string FormatEncryptedSection(string encryptedXml, string sectionName, string providerName) { return string.Format(CultureInfo.InvariantCulture, "<{0} {1}=\"{2}\"> {3} </{0}>", sectionName, "configProtectionProvider", providerName, encryptedXml); } internal static string EncryptSection(string clearXml, ProtectedConfigurationProvider provider) { XmlDocument xmlDocument = new XmlDocument { PreserveWhitespace = true }; xmlDocument.LoadXml(clearXml); XmlNode xmlNode = provider.Encrypt(xmlDocument.DocumentElement); return xmlNode.OuterXml; } } }