using System.Collections;
using System.Collections.Specialized;
using System.Xml;
+using System.Text;
using System.IO;
namespace System.Configuration
{
internal class SectionInfo: ConfigInfo
{
- public bool AllowLocation = true;
- public ConfigurationAllowDefinition AllowDefinition = ConfigurationAllowDefinition.Everywhere;
+ bool allowLocation = true;
+ bool? requirePermission = true;
+ ConfigurationAllowDefinition allowDefinition = ConfigurationAllowDefinition.Everywhere;
+ ConfigurationAllowExeDefinition allowExeDefinition = ConfigurationAllowExeDefinition.MachineToApplication;
public SectionInfo ()
{
}
- public SectionInfo (string sectionName, string typeName,
- bool allowLocation, ConfigurationAllowDefinition allowDefinition)
+ public SectionInfo (string sectionName, SectionInformation info)
{
Name = sectionName;
- TypeName = typeName;
- AllowLocation = allowLocation;
- AllowDefinition = allowDefinition;
+ TypeName = info.Type;
+ this.allowLocation = info.AllowLocation;
+ this.allowDefinition = info.AllowDefinition;
+ this.allowExeDefinition = info.AllowExeDefinition;
+ this.requirePermission = info.RequirePermission;
+ }
+
+ public override object CreateInstance ()
+ {
+ object ob = base.CreateInstance ();
+ ConfigurationSection sec = ob as ConfigurationSection;
+ if (sec != null) {
+ sec.SectionInformation.AllowLocation = allowLocation;
+ sec.SectionInformation.AllowDefinition = allowDefinition;
+ sec.SectionInformation.AllowExeDefinition = allowExeDefinition;
+ if (requirePermission != null)
+ sec.SectionInformation.RequirePermission = requirePermission.Value;
+ sec.SectionInformation.SetName (Name);
+ }
+ return ob;
}
public override bool HasDataContent (Configuration config)
public override void ReadConfig (Configuration cfg, string streamName, XmlTextReader reader)
{
- ConfigurationAllowDefinition allowDefinition;
StreamName = streamName;
ConfigHost = cfg.ConfigHost;
{
case "allowLocation":
string allowLoc = reader.Value;
- AllowLocation = (allowLoc == "true");
- if (!AllowLocation && allowLoc != "false")
+ allowLocation = (allowLoc == "true");
+ if (!allowLocation && allowLoc != "false")
ThrowException ("Invalid attribute value", reader);
break;
}
break;
+ case "allowExeDefinition":
+ string allowExeDef = reader.Value;
+ try {
+ allowExeDefinition = (ConfigurationAllowExeDefinition) Enum.Parse (
+ typeof (ConfigurationAllowExeDefinition), allowExeDef);
+ } catch {
+ ThrowException ("Invalid attribute value", reader);
+ }
+ break;
+
case "type":
TypeName = reader.Value;
break;
ThrowException ("location is a reserved section name", reader);
break;
+ case "requirePermission":
+ string reqPerm = reader.Value;
+ bool reqPermValue = (reqPerm == "true");
+ if (!reqPermValue && reqPerm != "false")
+ ThrowException ("Invalid attribute value", reader);
+ requirePermission = reqPermValue;
+ break;
+
default:
- ThrowException ("Unrecognized attribute.", reader);
+ ThrowException (String.Format ("Unrecognized attribute: {0}", reader.Name), reader);
break;
}
}
writer.WriteStartElement ("section");
writer.WriteAttributeString ("name", Name);
writer.WriteAttributeString ("type", TypeName);
- if (!AllowLocation)
+ if (!allowLocation)
writer.WriteAttributeString ("allowLocation", "false");
- if (AllowDefinition != ConfigurationAllowDefinition.Everywhere)
- writer.WriteAttributeString ("allowDefinition", AllowDefinition.ToString ());
+ if (allowDefinition != ConfigurationAllowDefinition.Everywhere)
+ writer.WriteAttributeString ("allowDefinition", allowDefinition.ToString ());
+ if (allowExeDefinition != ConfigurationAllowExeDefinition.MachineToApplication)
+ writer.WriteAttributeString ("allowExeDefinition", allowExeDefinition.ToString ());
+ if (requirePermission != null)
+ writer.WriteAttributeString ("requirePermission", requirePermission.Value ? "true" : "false");
writer.WriteEndElement ();
}
- public override void ReadData (Configuration config, XmlTextReader reader)
+ public override void ReadData (Configuration config, XmlTextReader reader, bool overrideAllowed)
{
+ if (!config.HasFile && !allowLocation)
+ throw new ConfigurationErrorsException ("The configuration section <" + Name + "> cannot be defined inside a <location> element.", reader);
+ if (!config.ConfigHost.IsDefinitionAllowed (config.ConfigPath, allowDefinition, allowExeDefinition)) {
+ object ctx = allowExeDefinition != ConfigurationAllowExeDefinition.MachineToApplication ? (object) allowExeDefinition : (object) allowDefinition;
+ throw new ConfigurationErrorsException ("The section <" + Name + "> can't be defined in this configuration file (the allowed definition context is '" + ctx + "').", reader);
+ }
+ if (config.GetSectionXml (this) != null)
+ ThrowException ("The section <" + Name + "> is defined more than once in the same configuration file.", reader);
config.SetSectionXml (this, reader.ReadOuterXml ());
}
if (section != null) {
ConfigurationSection parentSection = config.Parent != null ? config.Parent.GetSectionInstance (this, false) : null;
xml = section.SerializeSection (parentSection, Name, mode);
+
+ if (section.SectionInformation.IsProtected) {
+ StringBuilder sb = new StringBuilder ();
+ sb.AppendFormat ("<{0} configProtectionProvider=\"{1}\">\n",
+ Name,
+ section.SectionInformation.ProtectionProvider.Name);
+ sb.Append (config.ConfigHost.EncryptSection (xml,
+ section.SectionInformation.ProtectionProvider,
+ ProtectedConfiguration.Section));
+ sb.AppendFormat ("</{0}>", Name);
+ xml = sb.ToString ();
+ }
}
else {
xml = config.GetSectionXml (this);
}
if (xml != null) {
- XmlTextReader tr = new XmlTextReader (new StringReader (xml));
+ writer.WriteRaw (xml);
+/* XmlTextReader tr = new XmlTextReader (new StringReader (xml));
writer.WriteNode (tr, true);
- tr.Close ();
+ tr.Close ();*/
}
}
}