Ejemplo n.º 1
0
        // Helper method to parse ValidateIntegratedMode and verify correct
        private void CheckValidateIntegratedMode(System.Configuration.Configuration cfg)
        {
            IgnoreSection webServerSection = cfg.GetSection("system.webServer") as IgnoreSection;

            if (webServerSection != null)
            {
                SectionInformation sectionInformation = webServerSection.SectionInformation;
                string             rawXml             = sectionInformation == null ? null : sectionInformation.GetRawXml();
                Assert.IsFalse(string.IsNullOrEmpty(rawXml), "Did not expect empty system.webServer xml");

                XDocument xdoc = null;
                using (StringReader sr = new StringReader(rawXml))
                {
                    using (XmlReader xmlReader = XmlReader.Create(sr))
                    {
                        xdoc = XDocument.Load(xmlReader);
                    }
                }

                XElement xelem = xdoc.Element("system.webServer");
                Assert.IsNotNull(xelem, "system.webServer Xelement was null");

                xelem = xelem.Element("validation");
                Assert.IsNotNull(xelem, "system.webServer validation element was null");

                XAttribute attr = xelem.Attribute("validateIntegratedModeConfiguration");
                Assert.IsNotNull(attr, "system.webServer validateIntegratedMode attribute was null");
                Assert.AreEqual(attr.Value, "false", "validateIntegrateModel value was incorrect");
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Determines whether we need to add the "validateIntegratedModeConfiguration=false" attribute to the system.webServer section
        /// </summary>
        /// <remarks>This section is used by IIS in integrated mode and is necessary to deploy this domain service.</remarks>
        /// <returns><c>true</c> if we need to add validateIntegratedModeConfiguration to the system.webServer section.</returns>
        public bool DoWeNeedToValidateIntegratedModeToWebServer()
        {
            IgnoreSection webServerSection = this._configuration.GetSection(SystemWebServerSectionName) as IgnoreSection;

            if (webServerSection != null)
            {
                SectionInformation sectionInformation = webServerSection.SectionInformation;
                string             rawXml             = sectionInformation == null ? null : sectionInformation.GetRawXml();
                if (string.IsNullOrEmpty(rawXml))
                {
                    return(true);
                }

                XDocument xdoc = WebConfigUtil.CreateXDoc(rawXml);

                XElement xelem = xdoc.Element(SystemWebServerSectionName);
                if (xelem != null)
                {
                    xelem = xelem.Element(WebConfigUtil.ValidationSectionName);
                    XAttribute attr = xelem == null ? null : xelem.Attribute(WebConfigUtil.ValidateIntegratedModeConfigurationAttributeName);
                    return(attr == null ? true : !string.Equals(attr.Value, WebConfigUtil.FalseAttributeValue, StringComparison.OrdinalIgnoreCase));
                }
            }

            return(true);
        }
Ejemplo n.º 3
0
        public static void RegisterRedirectHandler(this EditableXmlConfigHelper configHelper, string name, string redirectHandlerPath, string fullTypeName)
        {
            // Because the "system.webServer" ConfigurationSection is an IgnoreSection, it must be modified
            // using the raw XML.

            ConfigurationSection webServerSection = configHelper.Configuration.GetSection(ConfigHelperExtensions.WebServerSectionName);

            if (webServerSection == null)
            {
                webServerSection = new IgnoreSection();
                configHelper.Configuration.Sections.Add(ConfigHelperExtensions.WebServerSectionName, webServerSection);
            }

            XElement webServerElement;
            string   webServerSectionRawXml = webServerSection.SectionInformation.GetRawXml();

            if (webServerSectionRawXml == null)
            {
                webServerElement = new XElement(ConfigHelperExtensions.WebServerSectionName);
            }
            else
            {
                webServerElement = XElement.Parse(webServerSectionRawXml);
            }

            XElement handlersElement = webServerElement.Element("handlers");

            if (handlersElement == null)
            {
                handlersElement = new XElement("handlers");
                webServerElement.Add(handlersElement);
            }

            XElement addElement = handlersElement.Elements("add")
                                  .FirstOrDefault(e =>
            {
                XAttribute attr = e.Attribute("type");
                return(attr != null && attr.Value == fullTypeName);
            });

            if (addElement == null)
            {
                addElement = new XElement("add",
                                          new XAttribute("name", name),
                                          new XAttribute("verb", "GET"),
                                          new XAttribute("path", redirectHandlerPath),
                                          new XAttribute("type", fullTypeName));
                handlersElement.Add(addElement);
            }

            webServerSection.SectionInformation.SetRawXml(webServerElement.ToString());
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Adds a module to the system.webServer section to point to our domain service module
        /// </summary>
        /// <param name="domainServiceModuleTypeName">Full type name of the domain service module</param>
        public void AddModuleToWebServer(string domainServiceModuleTypeName)
        {
            IgnoreSection      webServerSection   = this.GetOrCreateSystemWebServerSection();
            SectionInformation sectionInformation = webServerSection.SectionInformation;
            string             rawXml             = sectionInformation.GetRawXml();

            if (!string.IsNullOrEmpty(rawXml))
            {
                XDocument xdoc = WebConfigUtil.CreateXDoc(rawXml);

                XElement webSvrElement = xdoc.Element(SystemWebServerSectionName);
                XElement xelem         = webSvrElement.Element("modules");

                if (xelem == null)
                {
                    xelem = new XElement("modules");
                    webSvrElement.Add(xelem);
                }

                // Ensure we have the runAllManagedModulesForAllRequests attribute.
                // If it is present, we do not alter it
                XAttribute runAllManagedAttr = xelem.Attribute("runAllManagedModulesForAllRequests");
                if (runAllManagedAttr == null)
                {
                    runAllManagedAttr = new XAttribute("runAllManagedModulesForAllRequests", "true");
                    xelem.Add(runAllManagedAttr);
                }

                XElement newElem = new XElement("add",
                                                new XAttribute("name", BusinessLogicClassConstants.DomainServiceModuleName),
                                                new XAttribute("preCondition", BusinessLogicClassConstants.ManagedHandler),
                                                new XAttribute("type", domainServiceModuleTypeName));
                xelem.Add(newElem);

                rawXml = WebConfigUtil.CreateRawXml(xdoc);

                sectionInformation.SetRawXml(rawXml);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Retrieves or creates the system.webServer section
        /// </summary>
        /// <remarks>
        /// This section self initializes to empty xml, so if we detect that condition,
        /// we initialize it to empty but valid xml so that it can be manipulated.
        /// </remarks>
        /// <returns>The existing or new system.webServer section.</returns>
        public IgnoreSection GetOrCreateSystemWebServerSection()
        {
            // This section has no strongly typed equivalent, so we treat it only as an IgnoreSection
            IgnoreSection webServerSection = this._configuration.GetSection(SystemWebServerSectionName) as IgnoreSection;

            if (webServerSection == null)
            {
                webServerSection = new IgnoreSection();
                this._configuration.Sections.Add(SystemWebServerSectionName, webServerSection);
            }

            // Detect empty xml and initialize it to legal empty state if found.
            // We do this to simplify the logic of parsing it and adding new sections.
            SectionInformation sectionInformation = webServerSection.SectionInformation;
            string             rawXml             = sectionInformation.GetRawXml();

            if (string.IsNullOrEmpty(rawXml))
            {
                rawXml = "<" + SystemWebServerSectionName + "/>";
                sectionInformation.SetRawXml(rawXml);
            }
            return(webServerSection);
        }
Ejemplo n.º 6
0
        // Helper method to parse and check the module name in the system.webServer section
        private void CheckWebServerModule(System.Configuration.Configuration cfg, string moduleName)
        {
            IgnoreSection webServerSection = cfg.GetSection("system.webServer") as IgnoreSection;

            if (webServerSection != null)
            {
                SectionInformation sectionInformation = webServerSection.SectionInformation;
                string             rawXml             = sectionInformation == null ? null : sectionInformation.GetRawXml();
                Assert.IsFalse(string.IsNullOrEmpty(rawXml), "Did not expect empty system.webServer xml");

                XDocument xdoc = null;
                using (StringReader sr = new StringReader(rawXml))
                {
                    using (XmlReader xmlReader = XmlReader.Create(sr))
                    {
                        xdoc = XDocument.Load(xmlReader);
                    }
                }

                XElement xelem = xdoc.Element("system.webServer");
                Assert.IsNotNull(xelem, "system.webServer Xelement was null");

                xelem = xelem.Element("modules");
                Assert.IsNotNull(xelem, "system.webServer modules Xelement was null");

                XAttribute runAllManagedAttr = xelem.Attribute("runAllManagedModulesForAllRequests");
                Assert.IsNotNull(runAllManagedAttr, "Did not find attribute for runAllManagedModulesForAllRequests");
                Assert.AreEqual("true", runAllManagedAttr.Value, "runAllManagedModulesForAllRequests should have been true");

                IEnumerable <XElement> xelems = xelem.Elements("add");
                Assert.IsNotNull(xelems, "system.webServer modules add elements null");
                xelem = xelems.FirstOrDefault(e => (string)e.Attribute("name") == BusinessLogicClassConstants.DomainServiceModuleName);
                Assert.IsNotNull(xelem, "Did not find DomainServiceModule attribute");
                Assert.AreEqual(moduleName, (string)xelem.Attribute("type"), "DomainServiceModule name is incorrect");
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Adds the validateIntegratedModeConfiguration to the system.webServer/validation section
        /// </summary>
        public void AddValidateIntegratedModeToWebServer()
        {
            IgnoreSection webServerSection = this.GetOrCreateSystemWebServerSection();

            SectionInformation sectionInformation = webServerSection.SectionInformation;
            string             rawXml             = sectionInformation.GetRawXml();

            if (!string.IsNullOrEmpty(rawXml))
            {
                XDocument xdoc = WebConfigUtil.CreateXDoc(rawXml);

                XElement webSvrElement = xdoc.Element(SystemWebServerSectionName);
                XElement xelem         = webSvrElement.Element(WebConfigUtil.ValidationSectionName);

                if (xelem == null)
                {
                    xelem = new XElement(WebConfigUtil.ValidationSectionName);
                    webSvrElement.Add(xelem);
                }

                XAttribute attr = xelem.Attribute(WebConfigUtil.ValidateIntegratedModeConfigurationAttributeName);
                if (attr != null)
                {
                    attr.SetValue(WebConfigUtil.FalseAttributeValue);
                }
                else
                {
                    attr = new XAttribute(WebConfigUtil.ValidateIntegratedModeConfigurationAttributeName, WebConfigUtil.FalseAttributeValue);
                    xelem.Add(attr);
                }

                rawXml = WebConfigUtil.CreateRawXml(xdoc);

                sectionInformation.SetRawXml(rawXml);
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Determines whether we need to add a module to the system.webServer section
        /// </summary>
        /// <remarks>This module section is used by IIS in integrated mode and is necessary to deploy this domain service.</remarks>
        /// <returns><c>true</c> if we need to add a module to the system.webServer section.</returns>
        public bool DoWeNeedToAddModuleToWebServer()
        {
            IgnoreSection webServerSection = this._configuration.GetSection(SystemWebServerSectionName) as IgnoreSection;

            if (webServerSection != null)
            {
                SectionInformation sectionInformation = webServerSection.SectionInformation;
                string             rawXml             = sectionInformation == null ? null : sectionInformation.GetRawXml();
                if (string.IsNullOrEmpty(rawXml))
                {
                    return(true);
                }

                XDocument xdoc = WebConfigUtil.CreateXDoc(rawXml);

                // The logic is actually the following, but we null check to protect against malformed xml
                // xdoc.Element("system.webServer")
                //                    .Element("modules")
                //                    .Elements("add")
                //                    .Any(e => (string)e.Attribute("name") == BusinessLogicClassConstants.DomainServiceModuleName);
                XElement xelem = xdoc.Element(SystemWebServerSectionName);
                if (xelem != null)
                {
                    xelem = xelem.Element("modules");

                    if (xelem == null)
                    {
                        return(true);
                    }

                    IEnumerable <XElement> xelems = xelem.Elements("add");
                    return(xelems == null ? false : !xelems.Any(e => (string)e.Attribute("name") == BusinessLogicClassConstants.DomainServiceModuleName));
                }
            }
            return(true);
        }
Ejemplo n.º 9
0
        private static void AddWebServerModules(System.Configuration.Configuration config, string name, string type)
        {
            IgnoreSection ignoreSection = (IgnoreSection)config.GetSection("system.webServer");
            string        xml           = ignoreSection.SectionInformation.GetRawXml();
            bool          isChanged     = false;

            if (xml == null)
            {
                string temp = String.Format("<system.webServer><modules><add name=\"{0}\" type=\"{1}\" /></modules></system.webServer>", name, type);
                xml       = temp;
                isChanged = true;
            }
            else
            {
                xml = Regex.Replace(xml, "<modules\\s*/>", "<modules/>");
                xml = Regex.Replace(xml, "</modules\\s*>", "</modules>");
                xml = Regex.Replace(xml, "<modules\\s*>", "<modules>");
                xml = Regex.Replace(xml, "<system.webServer\\s*>", "<system.webServer>");
                xml = Regex.Replace(xml, "<system.webServer\\s*/>", "<system.webServer/>");
                xml = Regex.Replace(xml, "</system.webServer\\s*>", "</system.webServer>");

                if (xml.Contains("<modules/>"))
                {
                    string temp = String.Format("<modules><add name=\"{0}\" type=\"{1}\" /></modules>", name, type);
                    xml       = xml.Replace("<modules/>", temp.ToString());
                    isChanged = true;
                }
                else if (xml.Contains("<modules"))
                {
                    string temp = String.Format("<add name=\"{0}\" type=\"{1}\"", name, type);
                    if (!xml.Contains(temp))
                    {
                        temp += "/>";
                        int           position = xml.IndexOf("</modules>");
                        StringBuilder tempXml  = new StringBuilder(xml);
                        tempXml.Insert(position, temp, 1);
                        xml       = tempXml.ToString();
                        isChanged = true;
                    }
                }
                else if (xml.Contains("<system.webServer/>"))
                {
                    string temp = String.Format("<system.webServer><modules><add name=\"{0}\" type=\"{1}\" /></modules></system.webServer>", name, type);
                    xml       = xml.Replace("<system.webServer/>", temp);
                    isChanged = true;
                }
                else if (xml.Contains("<system.webServer"))
                {
                    string        temp     = String.Format("<modules><add name=\"{0}\" type=\"{1}\" /></modules>", name, type);
                    int           position = xml.IndexOf("</system.webServer>");
                    StringBuilder tempXml  = new StringBuilder(xml);
                    tempXml.Insert(position, temp, 1);
                    xml       = tempXml.ToString();
                    isChanged = true;
                }
            }

            if (isChanged)
            {
                ignoreSection.SectionInformation.SetRawXml(xml);
                config.Save();
            }
        }