Inheritance: System.Xml.XmlTextReader, IConfigErrorInfo
        static internal XmlNode CreateSectionXmlNode(ConfigXmlReader reader) {
            ErrorInfoXmlDocument doc = new ErrorInfoXmlDocument();
            doc.LoadFromConfigXmlReader(reader);
            XmlNode xmlNode = doc.DocumentElement;

            return xmlNode;
        }
Exemplo n.º 2
0
        internal static XmlNode CreateSectionXmlNode(ConfigXmlReader reader)
        {
            ErrorInfoXmlDocument document = new ErrorInfoXmlDocument();

            document.LoadFromConfigXmlReader(reader);
            return(document.DocumentElement);
        }
Exemplo n.º 3
0
            private object CreateSectionImpl(
                RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord,
                object parentConfig, ConfigXmlReader reader)
            {
                object config;

                if (_sectionCtor != null)
                {
                    ConfigurationSection configSection = (ConfigurationSection)TypeUtil.InvokeCtorWithReflectionPermission(_sectionCtor);

                    configSection.SectionInformation.SetRuntimeConfigurationInformation(configRecord, factoryRecord, sectionRecord);

                    configSection.CallInit();

                    ConfigurationSection parentSection = (ConfigurationSection)parentConfig;
                    configSection.Reset(parentSection);

                    if (reader != null)
                    {
                        configSection.DeserializeSection(reader);
                    }

                    // throw if there are any cached errors
                    ConfigurationErrorsException errors = configSection.GetErrors();
                    if (errors != null)
                    {
                        throw errors;
                    }

                    // don't allow changes to sections at runtime
                    configSection.SetReadOnly();

                    // reset the modified bit
                    configSection.ResetModified();

                    config = configSection;
                }
                else
                {
                    if (reader != null)
                    {
                        XmlNode xmlNode = ErrorInfoXmlDocument.CreateSectionXmlNode(reader);

                        CheckForLockAttributes(factoryRecord.ConfigKey, xmlNode);

                        // In v1, our old section handler expects a context that contains the virtualPath from the configPath
                        object configContext = configRecord.Host.CreateDeprecatedConfigContext(configRecord.ConfigPath);

                        config = _sectionHandler.Create(parentConfig, configContext, xmlNode);
                    }
                    else
                    {
                        config = null;
                    }
                }

                return(config);
            }
        // parentConfig contains the config that we'd merge with.
        override protected object CreateSection(bool inputIsTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, object parentConfig, ConfigXmlReader reader) {
            // Get the factory used to create a section.
            RuntimeConfigurationFactory factory = (RuntimeConfigurationFactory) factoryRecord.Factory;

            // Use the factory to create a section.
            object config = factory.CreateSection(inputIsTrusted, this, factoryRecord, sectionRecord, parentConfig, reader);

            return config;
        }
Exemplo n.º 5
0
        static internal XmlNode CreateSectionXmlNode(ConfigXmlReader reader)
        {
            ErrorInfoXmlDocument doc = new ErrorInfoXmlDocument();

            doc.LoadFromConfigXmlReader(reader);
            XmlNode xmlNode = doc.DocumentElement;

            return(xmlNode);
        }
Exemplo n.º 6
0
        protected internal override void DeserializeElement(XmlReader reader, bool serializeCollectionKey)
        {
            string name = reader.Name;

            base.DeserializeElement(reader, serializeCollectionKey);
            if ((this.File != null) && (this.File.Length > 0))
            {
                string file;
                string source = base.ElementInformation.Source;
                if (string.IsNullOrEmpty(source))
                {
                    file = this.File;
                }
                else
                {
                    file = Path.Combine(Path.GetDirectoryName(source), this.File);
                }
                if (System.IO.File.Exists(file))
                {
                    int    lineOffset = 0;
                    string rawXml     = null;
                    using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        using (XmlUtil util = new XmlUtil(stream, file, true))
                        {
                            if (util.Reader.Name != name)
                            {
                                throw new ConfigurationErrorsException(System.Configuration.SR.GetString("Config_name_value_file_section_file_invalid_root", new object[] { name }), util);
                            }
                            lineOffset = util.Reader.LineNumber;
                            rawXml     = util.CopySection();
                            while (!util.Reader.EOF)
                            {
                                if (util.Reader.NodeType != XmlNodeType.Comment)
                                {
                                    throw new ConfigurationErrorsException(System.Configuration.SR.GetString("Config_source_file_format"), util);
                                }
                                util.Reader.Read();
                            }
                        }
                    }
                    ConfigXmlReader reader2 = new ConfigXmlReader(rawXml, file, lineOffset);
                    reader2.Read();
                    if (reader2.MoveToNextAttribute())
                    {
                        throw new ConfigurationErrorsException(System.Configuration.SR.GetString("Config_base_unrecognized_attribute", new object[] { reader2.Name }), reader2);
                    }
                    reader2.MoveToElement();
                    base.DeserializeElement(reader2, serializeCollectionKey);
                }
            }
        }
 protected internal override void DeserializeElement(XmlReader reader, bool serializeCollectionKey)
 {
     string name = reader.Name;
     base.DeserializeElement(reader, serializeCollectionKey);
     if ((this.File != null) && (this.File.Length > 0))
     {
         string file;
         string source = base.ElementInformation.Source;
         if (string.IsNullOrEmpty(source))
         {
             file = this.File;
         }
         else
         {
             file = Path.Combine(Path.GetDirectoryName(source), this.File);
         }
         if (System.IO.File.Exists(file))
         {
             int lineOffset = 0;
             string rawXml = null;
             using (Stream stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read))
             {
                 using (XmlUtil util = new XmlUtil(stream, file, true))
                 {
                     if (util.Reader.Name != name)
                     {
                         throw new ConfigurationErrorsException(System.Configuration.SR.GetString("Config_name_value_file_section_file_invalid_root", new object[] { name }), util);
                     }
                     lineOffset = util.Reader.LineNumber;
                     rawXml = util.CopySection();
                     while (!util.Reader.EOF)
                     {
                         if (util.Reader.NodeType != XmlNodeType.Comment)
                         {
                             throw new ConfigurationErrorsException(System.Configuration.SR.GetString("Config_source_file_format"), util);
                         }
                         util.Reader.Read();
                     }
                 }
             }
             ConfigXmlReader reader2 = new ConfigXmlReader(rawXml, file, lineOffset);
             reader2.Read();
             if (reader2.MoveToNextAttribute())
             {
                 throw new ConfigurationErrorsException(System.Configuration.SR.GetString("Config_base_unrecognized_attribute", new object[] { reader2.Name }), reader2);
             }
             reader2.MoveToElement();
             base.DeserializeElement(reader2, serializeCollectionKey);
         }
     }
 }
        private void LoadFromConfigXmlReader(ConfigXmlReader reader) {
            IConfigErrorInfo err = (IConfigErrorInfo) reader;
            _filename = err.Filename;
            _lineOffset = err.LineNumber + 1;

            try {
                _reader = reader;
                base.Load(_reader);
            }
            finally {
                if (_reader != null) {
                    _reader.Close();
                    _reader = null;
                }
            }
        }
 private void LoadFromConfigXmlReader(ConfigXmlReader reader)
 {
     IConfigErrorInfo info = reader;
     this._filename = info.Filename;
     this._lineOffset = info.LineNumber + 1;
     try
     {
         this._reader = reader;
         base.Load(this._reader);
     }
     finally
     {
         if (this._reader != null)
         {
             this._reader.Close();
             this._reader = null;
         }
     }
 }
Exemplo n.º 10
0
        private void LoadFromConfigXmlReader(ConfigXmlReader reader)
        {
            IConfigErrorInfo err = (IConfigErrorInfo)reader;

            _filename   = err.Filename;
            _lineOffset = err.LineNumber + 1;

            try {
                _reader = reader;
                base.Load(_reader);
            }
            finally {
                if (_reader != null)
                {
                    _reader.Close();
                    _reader = null;
                }
            }
        }
Exemplo n.º 11
0
        private void LoadFromConfigXmlReader(ConfigXmlReader reader)
        {
            IConfigErrorInfo info = reader;

            this._filename   = info.Filename;
            this._lineOffset = info.LineNumber + 1;
            try
            {
                this._reader = reader;
                base.Load(this._reader);
            }
            finally
            {
                if (this._reader != null)
                {
                    this._reader.Close();
                    this._reader = null;
                }
            }
        }
Exemplo n.º 12
0
            private object CreateSectionWithRestrictedPermissions(
                RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord,
                object parentConfig, ConfigXmlReader reader)
            {
                // run configuration section handlers as if user code was on the stack
                bool revertPermitOnly = false;

                try {
                    PermissionSet permissionSet = configRecord.GetRestrictedPermissions();
                    if (permissionSet != null)
                    {
                        permissionSet.PermitOnly();
                        revertPermitOnly = true;
                    }

                    return(CreateSectionImpl(configRecord, factoryRecord, sectionRecord, parentConfig, reader));
                }
                finally {
                    if (revertPermitOnly)
                    {
                        CodeAccessPermission.RevertPermitOnly();
                    }
                }
            }
Exemplo n.º 13
0
 private object CreateSectionWithFullTrust(
     RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord,
     object parentConfig, ConfigXmlReader reader)
 {
     return(CreateSectionImpl(configRecord, factoryRecord, sectionRecord, parentConfig, reader));
 }
Exemplo n.º 14
0
            private object CreateSectionWithRestrictedPermissions(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord, object parentConfig, ConfigXmlReader reader)
            {
                object obj2;
                bool   flag = false;

                try
                {
                    PermissionSet restrictedPermissions = configRecord.GetRestrictedPermissions();
                    if (restrictedPermissions != null)
                    {
                        restrictedPermissions.PermitOnly();
                        flag = true;
                    }
                    obj2 = this.CreateSectionImpl(configRecord, factoryRecord, sectionRecord, parentConfig, reader);
                }
                finally
                {
                    if (flag)
                    {
                        CodeAccessPermission.RevertPermitOnly();
                    }
                }
                return(obj2);
            }
 private object CreateSectionImpl(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord, object parentConfig, ConfigXmlReader reader)
 {
     if (this._sectionCtor != null)
     {
         ConfigurationSection section = (ConfigurationSection) System.Configuration.TypeUtil.InvokeCtorWithReflectionPermission(this._sectionCtor);
         section.SectionInformation.SetRuntimeConfigurationInformation(configRecord, factoryRecord, sectionRecord);
         section.CallInit();
         ConfigurationSection parentElement = (ConfigurationSection) parentConfig;
         section.Reset(parentElement);
         if (reader != null)
         {
             section.DeserializeSection(reader);
         }
         ConfigurationErrorsException errors = section.GetErrors();
         if (errors != null)
         {
             throw errors;
         }
         section.SetReadOnly();
         section.ResetModified();
         return section;
     }
     if (reader != null)
     {
         XmlNode xmlNode = ErrorInfoXmlDocument.CreateSectionXmlNode(reader);
         CheckForLockAttributes(factoryRecord.ConfigKey, xmlNode);
         object configContext = configRecord.Host.CreateDeprecatedConfigContext(configRecord.ConfigPath);
         return this._sectionHandler.Create(parentConfig, configContext, xmlNode);
     }
     return null;
 }
 protected override object CreateSection(bool inputIsTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, object parentConfig, ConfigXmlReader reader)
 {
     RuntimeConfigurationFactory factory = (RuntimeConfigurationFactory) factoryRecord.Factory;
     return factory.CreateSection(inputIsTrusted, this, factoryRecord, sectionRecord, parentConfig, reader);
 }
Exemplo n.º 17
0
        protected internal override void DeserializeElement(XmlReader reader, bool serializeCollectionKey)
        {
            string elementName = reader.Name;

            base.DeserializeElement(reader, serializeCollectionKey);
            if (!(File?.Length > 0))
            {
                return;
            }

            string sourceFileFullPath;

            // Determine file location
            string configFile = ElementInformation.Source;

            sourceFileFullPath = string.IsNullOrEmpty(configFile)
                ? File
                : Path.Combine(Path.GetDirectoryName(configFile), File);

            if (!IO.File.Exists(sourceFileFullPath))
            {
                return;
            }
            int    lineOffset;
            string rawXml;

            using (Stream sourceFileStream = new FileStream(sourceFileFullPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                using (XmlUtil xmlUtil = new XmlUtil(sourceFileStream, sourceFileFullPath, true))
                {
                    if (xmlUtil.Reader.Name != elementName)
                    {
                        throw new ConfigurationErrorsException(
                                  SR.Format(SR.Config_name_value_file_section_file_invalid_root, elementName),
                                  xmlUtil);
                    }

                    lineOffset = xmlUtil.Reader.LineNumber;
                    rawXml     = xmlUtil.CopySection();

                    // Detect if there is any XML left over after the section
                    while (!xmlUtil.Reader.EOF)
                    {
                        XmlNodeType t = xmlUtil.Reader.NodeType;
                        if (t != XmlNodeType.Comment)
                        {
                            throw new ConfigurationErrorsException(SR.Config_source_file_format, xmlUtil);
                        }

                        xmlUtil.Reader.Read();
                    }
                }

            ConfigXmlReader internalReader = new ConfigXmlReader(rawXml, sourceFileFullPath, lineOffset);

            internalReader.Read();

            if (internalReader.MoveToNextAttribute())
            {
                throw new ConfigurationErrorsException(
                          SR.Format(SR.Config_base_unrecognized_attribute, internalReader.Name),
                          (XmlReader)internalReader);
            }

            internalReader.MoveToElement();

            base.DeserializeElement(internalReader, serializeCollectionKey);
        }
Exemplo n.º 18
0
        // parentConfig contains the config that we'd merge with.
        protected override object CreateSection(bool inputIsTrusted, FactoryRecord factoryRecord,
                                                SectionRecord sectionRecord, object parentConfig, ConfigXmlReader reader)
        {
            // Get the factory used to create a section.
            RuntimeConfigurationFactory factory = (RuntimeConfigurationFactory)factoryRecord.Factory;

            // Use the factory to create a section.
            object config = factory.CreateSection(inputIsTrusted, this, factoryRecord, sectionRecord, parentConfig,
                                                  reader);

            return(config);
        }
 internal static XmlNode CreateSectionXmlNode(ConfigXmlReader reader)
 {
     ErrorInfoXmlDocument document = new ErrorInfoXmlDocument();
     document.LoadFromConfigXmlReader(reader);
     return document.DocumentElement;
 }
        private ConfigXmlReader DecryptConfigSection(ConfigXmlReader reader, ProtectedConfigurationProvider protectionProvider) {
            ConfigXmlReader     clone           = reader.Clone();
            IConfigErrorInfo    err             = (IConfigErrorInfo)clone;
            string              encryptedXml    = null;
            string              clearTextXml    = null;
            XmlNodeType         nodeType;

            clone.Read();

            // Save the file and line at the top of the section
            
            string filename = err.Filename;
            int lineNumber = err.LineNumber;
            int sectionLineNumber = lineNumber;

            if (clone.IsEmptyElement) {
                throw new ConfigurationErrorsException(SR.GetString(SR.EncryptedNode_not_found), filename, lineNumber);
            }

            //////////////////////////////////////////////////////////
            // Find the <EncryptedData> node
            for (;;) {
                clone.Read(); // Keep reading till we find a relavant node
                
                nodeType = clone.NodeType;
                
                if (nodeType  == XmlNodeType.Element && clone.Name == "EncryptedData")  { // Found it!
                    break;
                }

                if (nodeType == XmlNodeType.EndElement) {
                    throw new ConfigurationErrorsException(SR.GetString(SR.EncryptedNode_not_found), filename, lineNumber);
                }
                else if (nodeType  != XmlNodeType.Comment && nodeType != XmlNodeType.Whitespace) {
                    // some other unexpected content
                    throw new ConfigurationErrorsException(SR.GetString(SR.EncryptedNode_is_in_invalid_format), filename, lineNumber);
                }
            }

            //////////////////////////////////////////////////////////
            // Do the decryption

            // Save the line at the top of the <EncryptedData> node
            lineNumber = err.LineNumber;

            encryptedXml = clone.ReadOuterXml();
            try {
                clearTextXml = CallHostDecryptSection(encryptedXml, protectionProvider, ProtectedConfig);
            } catch (Exception e) {
                throw new ConfigurationErrorsException(SR.GetString(SR.Decryption_failed, protectionProvider.Name, e.Message), e, filename, lineNumber);
            }
            catch {
                throw new ConfigurationErrorsException(SR.GetString(SR.Decryption_failed, protectionProvider.Name, ExceptionUtil.NoExceptionInformation), filename, lineNumber);
            }

            // Detect if there is any XML left over after <EncryptedData>
            do {
                nodeType = clone.NodeType;
                
                if (nodeType == XmlNodeType.EndElement) {
                    break;
                }
                else if (nodeType  != XmlNodeType.Comment && nodeType != XmlNodeType.Whitespace) {
                    // Got other unexpected content
                    throw new ConfigurationErrorsException(SR.GetString(SR.EncryptedNode_is_in_invalid_format), filename, lineNumber);
                }
            } while (clone.Read());
            
            // Create a new reader, using the position of the original reader
            return new ConfigXmlReader(clearTextXml, filename, sectionLineNumber, true);
        }
        protected object CallCreateSection(bool inputIsTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, object parentConfig, ConfigXmlReader reader, string filename, int line) {
            object config;

            // Call into config section while impersonating process or UNC identity
            // so that the section could read files from disk if needed
            try {
                using (Impersonate()) {
                    config = CreateSection(inputIsTrusted, factoryRecord, sectionRecord, parentConfig, reader);
                    if (config == null && parentConfig != null) {
                        throw new ConfigurationErrorsException(SR.GetString(SR.Config_object_is_null), filename, line);
                    }
                }
            }
            catch (Exception e) {
                throw ExceptionUtil.WrapAsConfigException(SR.GetString(SR.Config_exception_creating_section_handler, factoryRecord.ConfigKey), e, filename, line);
            }
            catch {
                throw ExceptionUtil.WrapAsConfigException(SR.GetString(SR.Config_exception_creating_section_handler, factoryRecord.ConfigKey), null, filename, line);
            }

            return config;
        }
 // Create the configuration object
 protected abstract object CreateSection(bool inputIsTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, object parentConfig, ConfigXmlReader reader);
        protected ConfigXmlReader GetSectionXmlReader(string[] keys, SectionInput input) {
            ConfigXmlReader reader = null;
            string filename = input.SectionXmlInfo.Filename;
            int lineNumber = input.SectionXmlInfo.LineNumber;

            try {
                string name = keys[keys.Length-1];
                string rawXml = input.SectionXmlInfo.RawXml;
                if (rawXml != null) {
                    // Use the stored raw xml to provide the content of the section.
                    reader = new ConfigXmlReader(rawXml, input.SectionXmlInfo.Filename, input.SectionXmlInfo.LineNumber);
                }
                else if (!String.IsNullOrEmpty(input.SectionXmlInfo.ConfigSource)) {
                    // Load the  config source to provide the content of the section.
                    filename = input.SectionXmlInfo.ConfigSourceStreamName;
                    lineNumber = 0;
                    reader = LoadConfigSource(name, input.SectionXmlInfo);
                }
                else {
                    // Find the content of the section in the config file.
                    lineNumber = 0;
                    reader = FindSection(keys, input.SectionXmlInfo, out lineNumber);
                }

                if (reader != null) {
                    if (!input.IsProtectionProviderDetermined) {
                        input.ProtectionProvider = GetProtectionProviderFromName(input.SectionXmlInfo.ProtectionProviderName, false);
                    }

                    if (input.ProtectionProvider != null) {
                        reader = DecryptConfigSection(reader, input.ProtectionProvider);
                    }
                }
            }

            //
            // Guarantee that exceptions contain the name of the stream and an approximate line number.
            //
            catch (Exception e) {
                throw ExceptionUtil.WrapAsConfigException(SR.GetString(SR.Config_error_loading_XML_file), e, filename, lineNumber);
            } 
            catch {
                throw ExceptionUtil.WrapAsConfigException(SR.GetString(SR.Config_error_loading_XML_file), null, filename, lineNumber);
            }

            return reader;
        }
        private ConfigXmlReader LoadConfigSource(string name, SectionXmlInfo sectionXmlInfo) {
            string configSourceStreamName = sectionXmlInfo.ConfigSourceStreamName;
            
            try {
                using (Impersonate()) {
                    using (Stream stream = Host.OpenStreamForRead(configSourceStreamName)) {
                        if (stream == null) {
                            throw new ConfigurationErrorsException(
                                    SR.GetString(SR.Config_cannot_open_config_source, sectionXmlInfo.ConfigSource),
                                    sectionXmlInfo);
                        }

                        using (XmlUtil xmlUtil = new XmlUtil(stream, configSourceStreamName, true)) {
                            if (xmlUtil.Reader.Name != name) {
                                throw new ConfigurationErrorsException(SR.GetString(SR.Config_source_file_format), xmlUtil);
                            }

                            // Check for protectionProvider
                            string protectionProviderAttribute = xmlUtil.Reader.GetAttribute(KEYWORD_PROTECTION_PROVIDER);
                            if (protectionProviderAttribute != null) {
                                if (xmlUtil.Reader.AttributeCount != 1) {
                                    // Error: elements with protectionProvider should not have other attributes
                                    throw new ConfigurationErrorsException(SR.GetString(SR.Protection_provider_syntax_error), xmlUtil);
                                }
        
                                sectionXmlInfo.ProtectionProviderName = ValidateProtectionProviderAttribute(protectionProviderAttribute, xmlUtil);
                            }
                            
                            int lineOffset = xmlUtil.Reader.LineNumber;
                            string rawXml = xmlUtil.CopySection();

                            // Detect if there is any XML left over after the section
                            while (!xmlUtil.Reader.EOF) {
                                XmlNodeType t = xmlUtil.Reader.NodeType;
                                if (t != XmlNodeType.Comment) {
                                    throw new ConfigurationErrorsException(SR.GetString(SR.Config_source_file_format), xmlUtil);
                                }

                                xmlUtil.Reader.Read();
                            }

                            ConfigXmlReader section = new ConfigXmlReader(rawXml, configSourceStreamName, lineOffset);
                            return section;
                        }
                    }
                }
            }
            catch {
                // Don't allow frames up the stack to run exception filters while impersonated.
                throw;
            }
        }
        private ConfigXmlReader FindSectionRecursive(string [] keys, int iKey, XmlUtil xmlUtil, ref int lineNumber) {
            string name = keys[iKey];
            ConfigXmlReader section = null;

            int depth = xmlUtil.Reader.Depth;
            xmlUtil.ReadToNextElement();

            while (xmlUtil.Reader.Depth > depth) {
                if (xmlUtil.Reader.Name == name) {
                    if (iKey < keys.Length - 1) {
                        //
                        // We haven't reached the section yet, so keep evaluating
                        //
                        section = FindSectionRecursive(keys, iKey + 1, xmlUtil, ref lineNumber);
                        if (section != null) {
                            break;
                        }

                        continue; // don't call "Skip" -- FindSectionRecursive forwards the reader
                    }
                    else {
                        //
                        // We've reached the section. Load the section into a string.
                        //
                        string filename = ((IConfigErrorInfo)xmlUtil).Filename;
                        int lineOffset = xmlUtil.Reader.LineNumber;
                        string rawXml = xmlUtil.CopySection();
                        section = new ConfigXmlReader(rawXml, filename, lineOffset);
                        break;
                    }
                }
                else if (iKey == 0 && xmlUtil.Reader.Name == KEYWORD_LOCATION) {
                    string locationSubPath = xmlUtil.Reader.GetAttribute(KEYWORD_LOCATION_PATH);
                    bool isValid = false;
                    try {
                        locationSubPath = NormalizeLocationSubPath(locationSubPath, xmlUtil);
                        isValid = true;
                    }
                    catch (ConfigurationException ce) {
                        xmlUtil.SchemaErrors.AddError(ce, ExceptionAction.NonSpecific);
                    }

                    if (isValid && locationSubPath == null) {
                        //
                        // Location sections that don't have a subpath are treated
                        // as ordinary sections.
                        //
                        section = FindSectionRecursive(keys, iKey, xmlUtil, ref lineNumber);
                        if (section != null) {
                            break;
                        }

                        continue; // don't call "Skip" -- FindSectionRecursive forwards the reader
                    }
                }

                xmlUtil.SkipToNextElement();
            }

            return section;
        }
Exemplo n.º 26
0
 internal object CreateSection(bool inputIsTrusted, RuntimeConfigurationRecord configRecord,
                               FactoryRecord factoryRecord, SectionRecord sectionRecord, object parentConfig, ConfigXmlReader reader)
 {
     if (inputIsTrusted)
     {
         return(CreateSectionWithFullTrust(configRecord, factoryRecord, sectionRecord, parentConfig, reader));
     }
     else
     {
         return(CreateSectionWithRestrictedPermissions(configRecord, factoryRecord, sectionRecord, parentConfig, reader));
     }
 }
Exemplo n.º 27
0
 private object CreateSectionImpl(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord, object parentConfig, ConfigXmlReader reader)
 {
     if (this._sectionCtor != null)
     {
         ConfigurationSection section = (ConfigurationSection)System.Configuration.TypeUtil.InvokeCtorWithReflectionPermission(this._sectionCtor);
         section.SectionInformation.SetRuntimeConfigurationInformation(configRecord, factoryRecord, sectionRecord);
         section.CallInit();
         ConfigurationSection parentElement = (ConfigurationSection)parentConfig;
         section.Reset(parentElement);
         if (reader != null)
         {
             section.DeserializeSection(reader);
         }
         ConfigurationErrorsException errors = section.GetErrors();
         if (errors != null)
         {
             throw errors;
         }
         section.SetReadOnly();
         section.ResetModified();
         return(section);
     }
     if (reader != null)
     {
         XmlNode xmlNode = ErrorInfoXmlDocument.CreateSectionXmlNode(reader);
         CheckForLockAttributes(factoryRecord.ConfigKey, xmlNode);
         object configContext = configRecord.Host.CreateDeprecatedConfigContext(configRecord.ConfigPath);
         return(this._sectionHandler.Create(parentConfig, configContext, xmlNode));
     }
     return(null);
 }
Exemplo n.º 28
0
        protected internal override void DeserializeElement(XmlReader reader, bool serializeCollectionKey)
        {
            string elementName = reader.Name;

            base.DeserializeElement(reader, serializeCollectionKey);
            if (!(File?.Length > 0)) return;

            string sourceFileFullPath;

            // Determine file location
            string configFile = ElementInformation.Source;

            if (string.IsNullOrEmpty(configFile))
                sourceFileFullPath = File;
            else
            {
                string configFileDirectory = Path.GetDirectoryName(configFile);
                sourceFileFullPath = Path.Combine(configFileDirectory, File);
            }

            if (!IO.File.Exists(sourceFileFullPath)) return;
            int lineOffset;
            string rawXml;

            using (
                Stream sourceFileStream = new FileStream(sourceFileFullPath, FileMode.Open, FileAccess.Read,
                    FileShare.Read))
            {
                using (XmlUtil xmlUtil = new XmlUtil(sourceFileStream, sourceFileFullPath, true))
                {
                    if (xmlUtil.Reader.Name != elementName)
                    {
                        throw new ConfigurationErrorsException(
                            string.Format(SR.Config_name_value_file_section_file_invalid_root, elementName),
                            xmlUtil);
                    }

                    lineOffset = xmlUtil.Reader.LineNumber;
                    rawXml = xmlUtil.CopySection();

                    // Detect if there is any XML left over after the section
                    while (!xmlUtil.Reader.EOF)
                    {
                        XmlNodeType t = xmlUtil.Reader.NodeType;
                        if (t != XmlNodeType.Comment)
                        {
                            throw new ConfigurationErrorsException(SR.Config_source_file_format,
                                xmlUtil);
                        }

                        xmlUtil.Reader.Read();
                    }
                }
            }

            ConfigXmlReader internalReader = new ConfigXmlReader(rawXml, sourceFileFullPath, lineOffset);
            internalReader.Read();
            if (internalReader.MoveToNextAttribute())
            {
                throw new ConfigurationErrorsException(
                    string.Format(SR.Config_base_unrecognized_attribute, internalReader.Name),
                    (XmlReader)internalReader);
            }

            internalReader.MoveToElement();

            base.DeserializeElement(internalReader, serializeCollectionKey);
        }
            private object CreateSectionWithFullTrust(
                    RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord, 
                    object parentConfig, ConfigXmlReader reader) {

                return CreateSectionImpl(configRecord, factoryRecord, sectionRecord, parentConfig, reader);
            }
            internal object CreateSection(bool inputIsTrusted, RuntimeConfigurationRecord configRecord, 
                    FactoryRecord factoryRecord, SectionRecord sectionRecord, object parentConfig, ConfigXmlReader reader) {

                if (inputIsTrusted) {
                    return CreateSectionWithFullTrust(configRecord, factoryRecord, sectionRecord, parentConfig, reader);
                }
                else {
                    return CreateSectionWithRestrictedPermissions(configRecord, factoryRecord, sectionRecord, parentConfig, reader);
                }
            }
 private object CreateSectionWithRestrictedPermissions(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord, object parentConfig, ConfigXmlReader reader)
 {
     object obj2;
     bool flag = false;
     try
     {
         PermissionSet restrictedPermissions = configRecord.GetRestrictedPermissions();
         if (restrictedPermissions != null)
         {
             restrictedPermissions.PermitOnly();
             flag = true;
         }
         obj2 = this.CreateSectionImpl(configRecord, factoryRecord, sectionRecord, parentConfig, reader);
     }
     finally
     {
         if (flag)
         {
             CodeAccessPermission.RevertPermitOnly();
         }
     }
     return obj2;
 }
            private object CreateSectionImpl(
                    RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord, 
                    object parentConfig, ConfigXmlReader reader) {

                object config;

                if (_sectionCtor != null) {
                    ConfigurationSection configSection = (ConfigurationSection) TypeUtil.InvokeCtorWithReflectionPermission(_sectionCtor);

                    configSection.SectionInformation.SetRuntimeConfigurationInformation(configRecord, factoryRecord, sectionRecord);

                    configSection.CallInit();

                    ConfigurationSection parentSection = (ConfigurationSection) parentConfig;
                    configSection.Reset(parentSection);

                    if (reader != null) {
                        configSection.DeserializeSection(reader);
                    }

                    // throw if there are any cached errors
                    ConfigurationErrorsException errors = configSection.GetErrors();
                    if (errors != null) {
                        throw errors;
                    }

                    // don't allow changes to sections at runtime
                    configSection.SetReadOnly();

                    // reset the modified bit
                    configSection.ResetModified();

                    config = configSection;
                }
                else {
                    if (reader != null) {
                        XmlNode xmlNode = ErrorInfoXmlDocument.CreateSectionXmlNode(reader);

                        CheckForLockAttributes(factoryRecord.ConfigKey, xmlNode);

                        // In v1, our old section handler expects a context that contains the virtualPath from the configPath
                        object configContext = configRecord.Host.CreateDeprecatedConfigContext(configRecord.ConfigPath);

                        config = _sectionHandler.Create(parentConfig, configContext, xmlNode);
                    }
                    else {
                        config = null;
                    }
                }

                return config;
            }
 protected override object CreateSection(bool inputIsTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, object parentConfig, ConfigXmlReader reader)
 {
     ConstructorInfo factory = (ConstructorInfo) factoryRecord.Factory;
     ConfigurationSection section = (ConfigurationSection) System.Configuration.TypeUtil.InvokeCtorWithReflectionPermission(factory);
     section.SectionInformation.AttachToConfigurationRecord(this, factoryRecord, sectionRecord);
     section.CallInit();
     ConfigurationSection parentElement = (ConfigurationSection) parentConfig;
     section.Reset(parentElement);
     if (reader != null)
     {
         section.DeserializeSection(reader);
     }
     section.ResetModified();
     return section;
 }
            private object CreateSectionWithRestrictedPermissions(
                    RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord, 
                    object parentConfig, ConfigXmlReader reader) {

                // run configuration section handlers as if user code was on the stack
                bool revertPermitOnly = false;
                try {
                    PermissionSet permissionSet = configRecord.GetRestrictedPermissions();
                    if (permissionSet != null) {
                        permissionSet.PermitOnly();
                        revertPermitOnly = true;
                    }

                    return CreateSectionImpl(configRecord, factoryRecord, sectionRecord, parentConfig, reader);
                }
                finally {
                    if (revertPermitOnly) {
                        CodeAccessPermission.RevertPermitOnly();
                    }
                }
            }
 private object CreateSectionWithRestrictedPermissions(
         RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord, 
         object parentConfig, ConfigXmlReader reader)
 {
     return CreateSectionImpl(configRecord, factoryRecord, sectionRecord, parentConfig, reader);
 }
        //
        // Create the ConfigurationSection.
        //
        override protected object CreateSection(bool inputIsTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, object parentConfig, ConfigXmlReader reader) {
            // Create an instance of the ConfigurationSection
            ConstructorInfo ctor = (ConstructorInfo) factoryRecord.Factory;
            ConfigurationSection configSection = (ConfigurationSection) TypeUtil.InvokeCtorWithReflectionPermission(ctor);

            // Attach the ConfigurationSection to this record
            configSection.SectionInformation.AttachToConfigurationRecord(this, factoryRecord, sectionRecord);
            configSection.CallInit();

            // Initialize the ConfigurationSection with XML or just its parent.
            ConfigurationSection parentConfigSection = (ConfigurationSection) parentConfig;
            configSection.Reset(parentConfigSection);
            if (reader != null) {
                configSection.DeserializeSection(reader);
            }

            // Clear the modified bit.
            configSection.ResetModified();

            return configSection;
        }
        //
        // Update the section with the XML provided.
        //
        // This method will throw out any changes made to the section up to this point.
        //
        // If xmlElement is null or empty, it is equivalent to calling RevertToParent
        //
        internal void SetRawXml(ConfigurationSection configSection, string xmlElement) {

            // Null or empty is equivalent to RevertToParent().
            if (string.IsNullOrEmpty(xmlElement)) {
                RevertToParent(configSection);
                return;
            }

            ValidateSectionXml(xmlElement, configSection.SectionInformation.Name);

            // Reset the ConfigurationSection with the XML.
            ConfigurationSection parentConfigSection = FindImmediateParentSection(configSection);
            ConfigXmlReader reader = new ConfigXmlReader(xmlElement, null, 0);

            // Store the raw XML.
            configSection.SectionInformation.RawXml = xmlElement;

            // Update the section with the xml
            try {
                try {
                    bool wasPresent = configSection.ElementPresent;
                    PropertySourceInfo saveInfo = configSection.ElementInformation.PropertyInfoInternal();

                    configSection.Reset(parentConfigSection);
                    configSection.DeserializeSection(reader);
                    configSection.ResetModified();

                    configSection.ElementPresent = wasPresent;
                    configSection.ElementInformation.ChangeSourceAndLineNumber(saveInfo);
                }
                catch {
                    configSection.SectionInformation.RawXml = null;
                    throw;
                }
            }
            catch (Exception e) {
                throw new ConfigurationErrorsException(SR.GetString(SR.Config_exception_in_config_section_handler, configSection.SectionInformation.SectionName),
                        e, null, 0);
            }

            // Ignore previous attempts to remove the section.
            configSection.SectionInformation.Removed = false;
        }
 internal void SetRawXml(ConfigurationSection configSection, string xmlElement)
 {
     if (string.IsNullOrEmpty(xmlElement))
     {
         this.RevertToParent(configSection);
     }
     else
     {
         this.ValidateSectionXml(xmlElement, configSection.SectionInformation.Name);
         ConfigurationSection parentElement = this.FindImmediateParentSection(configSection);
         ConfigXmlReader reader = new ConfigXmlReader(xmlElement, null, 0);
         configSection.SectionInformation.RawXml = xmlElement;
         try
         {
             try
             {
                 bool elementPresent = configSection.ElementPresent;
                 PropertySourceInfo sourceInformation = configSection.ElementInformation.PropertyInfoInternal();
                 configSection.Reset(parentElement);
                 configSection.DeserializeSection(reader);
                 configSection.ResetModified();
                 configSection.ElementPresent = elementPresent;
                 configSection.ElementInformation.ChangeSourceAndLineNumber(sourceInformation);
             }
             catch
             {
                 configSection.SectionInformation.RawXml = null;
                 throw;
             }
         }
         catch (Exception exception)
         {
             throw new ConfigurationErrorsException(System.Configuration.SR.GetString("Config_exception_in_config_section_handler", new object[] { configSection.SectionInformation.SectionName }), exception, null, 0);
         }
         configSection.SectionInformation.Removed = false;
     }
 }
Exemplo n.º 39
0
        protected override object CreateSection(bool inputIsTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, object parentConfig, ConfigXmlReader reader)
        {
            RuntimeConfigurationFactory factory = (RuntimeConfigurationFactory)factoryRecord.Factory;

            return(factory.CreateSection(inputIsTrusted, this, factoryRecord, sectionRecord, parentConfig, reader));
        }