2 // System.Configuration.ProtectedConfigurationSection.cs
5 // Duncan Mak (duncan@ximian.com)
6 // Chris Toshok (toshok@ximian.com)
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
35 namespace System.Configuration
37 public sealed class ProtectedConfigurationSection: ConfigurationSection
39 static ConfigurationProperty defaultProviderProp;
40 static ConfigurationProperty providersProp;
41 static ConfigurationPropertyCollection properties;
43 ProtectedConfigurationProviderCollection providers;
45 static ProtectedConfigurationSection ()
47 defaultProviderProp = new ConfigurationProperty ("defaultProvider", typeof (string), "RsaProtectedConfigurationProvider");
48 providersProp = new ConfigurationProperty ("providers", typeof (ProviderSettingsCollection), null);
50 properties = new ConfigurationPropertyCollection();
51 properties.Add (defaultProviderProp);
52 properties.Add (providersProp);
55 [ConfigurationProperty ("defaultProvider", DefaultValue="RsaProtectedConfigurationProvider")]
56 public string DefaultProvider {
57 get { return (string)base[defaultProviderProp]; }
58 set { base[defaultProviderProp] = value; }
61 [ConfigurationProperty ("providers")]
62 public ProviderSettingsCollection Providers {
63 get { return (ProviderSettingsCollection)base[providersProp]; }
66 protected internal override ConfigurationPropertyCollection Properties {
67 get { return properties; }
70 internal string EncryptSection (string clearXml, ProtectedConfigurationProvider protectionProvider)
72 XmlDocument doc = new XmlDocument ();
73 doc.LoadXml (clearXml);
75 XmlNode encryptedNode = protectionProvider.Encrypt (doc.DocumentElement);
77 return encryptedNode.OuterXml;
80 internal string DecryptSection (string encryptedXml, ProtectedConfigurationProvider protectionProvider)
82 XmlDocument doc = new XmlDocument ();
83 doc.InnerXml = encryptedXml;
85 XmlNode decryptedNode = protectionProvider.Decrypt (doc.DocumentElement);
87 return decryptedNode.OuterXml;
90 internal ProtectedConfigurationProviderCollection GetAllProviders ()
92 if (providers == null) {
93 providers = new ProtectedConfigurationProviderCollection ();
95 foreach (ProviderSettings ps in Providers) {
96 providers.Add (InstantiateProvider (ps));
103 ProtectedConfigurationProvider InstantiateProvider (ProviderSettings ps)
105 Type t = Type.GetType (ps.Type, true);
106 ProtectedConfigurationProvider prov = Activator.CreateInstance (t) as ProtectedConfigurationProvider;
108 throw new Exception ("The type specified does not extend ProtectedConfigurationProvider class.");
110 prov.Initialize (ps.Name, ps.Parameters);