Пример #1
0
        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            var webApp = properties.Feature.Parent as SPWebApplication;

            WebModifications.CreateWildcardNode(true, webApp);
            WebModifications.CreateProviderNode(true, webApp);
            WebModifications.CreateStsProviderNode(true, properties);
            WebModifications.CreateAdminWildcardNode(true, webApp);
            WebModifications.CreateAdminProviderNode(true, webApp);

            var local = SPFarm.Local;

            var services = from s in local.Services
                           where s.Name == "SPTimerV4"
                           select s;

            var service = services.First();

            foreach (var job in service.JobDefinitions)
            {
                if (job.Name == tJobName)
                {
                    if (job.IsDisabled)
                    {
                        job.IsDisabled = false;
                    }
                    job.Update();
                    job.RunNow();
                }
            }

            //Remove the Forms Authentication provider for the Web Application
            try
            {
                foreach (var provider in
                         webApp.IisSettings[SPUrlZone.Default].ClaimsAuthenticationProviders)
                {
                    if (provider.ClaimProviderName == "Forms")
                    {
                        webApp.IisSettings[SPUrlZone.Default].DeleteClaimsAuthenticationProvider(provider);
                        webApp.Update();
                        break;
                    }
                }
            }
            catch (ArgumentNullException)
            {
                //Forms provider already removed
            }
            catch (ArgumentException)
            {
                //Claims provider is null
            }
            finally
            {
                webApp.IisSettings[SPUrlZone.Default].ClaimsAuthenticationRedirectionUrl = null;
                webApp.Update();
            }
        }
        protected void RemoveFbaSettings(SPWebApplication webApp, SPFeatureReceiverProperties properties)
        {
            using (SPSite siteCollection = new SPSite(SPContext.Current.Site.ID))
            {
                using (SPWeb site = siteCollection.OpenWeb())
                {
                    try
                    {
                        SPList list = site.Lists.TryGetList("Nauplius.ADLDS.FBA - WebApplicationSettings");

                        if (list != null)
                        {
                            SPListItemCollection items = list.Items;

                            foreach (SPListItem item in items)
                            {
                                var zone = GetZone(item);

                                if (item["WebApplicationUrl"].ToString() == webApp.GetResponseUri(zone).AbsoluteUri)
                                {
                                    //Remove the Forms Authentication provider for the Web Application
                                    try
                                    {
                                        foreach (var provider in
                                                 webApp.IisSettings[zone].ClaimsAuthenticationProviders)
                                        {
                                            if (provider.ClaimProviderName == "Forms")
                                            {
                                                webApp.IisSettings[zone].DeleteClaimsAuthenticationProvider(provider);
                                                webApp.Update();
                                                webApp.ProvisionGlobally();
                                                break;
                                            }
                                        }
                                    }
                                    catch (ArgumentNullException)
                                    {
                                        //Forms provider already removed
                                    }
                                    catch (ArgumentException)
                                    {
                                        //Claims provider is null
                                    }
                                    finally
                                    {
                                        webApp.IisSettings[zone].ClaimsAuthenticationRedirectionUrl = null;
                                        webApp.Update();
                                        webApp.ProvisionGlobally();
                                    }

                                    WebModifications.CreateWildcardNode(true, webApp, zone);
                                    WebModifications.CreateProviderNode(true, webApp, zone);
                                    WebModifications.CreateStsProviderNode(true, properties, zone);
                                    WebModifications.CreateAdminWildcardNode(true, webApp, zone);
                                    WebModifications.CreateAdminProviderNode(true, webApp, zone);

                                    var local = SPFarm.Local;

                                    var services = from s in local.Services
                                                   where s.Name == "SPTimerV4"
                                                   select s;

                                    var service = services.First();

                                    foreach (var job in service.JobDefinitions)
                                    {
                                        if (job.Name == tJobName)
                                        {
                                            if (job.IsDisabled)
                                            {
                                                job.IsDisabled = false;
                                            }
                                            job.Update();
                                            job.RunNow();
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch (SPException ex)
                    {
                        Logging.LogMessage(951, Logging.LogCategories.STSXML, TraceSeverity.Unexpected,
                                           String.Format("Unable to update the WebApplicationSettings List in Central Administration. {0}",
                                                         ex.StackTrace),
                                           new object[] { null });
                        throw new SPException(@"Unable to update the WebApplicationSettings List in Central Administration.  Validate the list exists.");
                    }
                }
            }
        }
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            var webApp = properties.Feature.Parent as SPWebApplication;

            //Build MasterXmlFragment if SPListItem is blank or missing attributes
            using (SPSite siteCollection = new SPSite(SPContext.Current.Site.ID))
            {
                using (SPWeb site = siteCollection.OpenWeb())
                {
                    try
                    {
                        SPList list = site.Lists.TryGetList("Nauplius.ADLDS.FBA - StsFarm");
                        if (list != null)
                        {
                            if (list.ItemCount >= 1)
                            {
                                foreach (SPListItem item in list.Items)
                                {
                                    if (item["StsConfig"].ToString() == "MasterXmlFragment")
                                    {
                                        try
                                        {
                                            MasterXmlFragment.LoadXml((string)item["XMLStsConfig"]);
                                        }
                                        catch (XmlException)
                                        {
                                            MasterXmlFragment.AppendChild(
                                                MasterXmlFragment.CreateNode(XmlNodeType.Element, "system.web", ""));
                                        }

                                        if (MasterXmlFragment.SelectSingleNode(@"system.web/membership") == null)
                                        {
                                            CreateStsXPath(MasterXmlFragment, item, "system.web/membership");
                                        }

                                        if (MasterXmlFragment.SelectSingleNode(@"system.web/membership/providers") ==
                                            null)
                                        {
                                            CreateStsXPath(MasterXmlFragment, item, "system.web/membership/providers");
                                        }

                                        if (MasterXmlFragment.SelectSingleNode(@"system.web/roleManager") == null)
                                        {
                                            CreateStsXPath(MasterXmlFragment, item, "system.web/roleManager");
                                        }

                                        if (MasterXmlFragment.SelectSingleNode(@"system.web/roleManager/providers") ==
                                            null)
                                        {
                                            CreateStsXPath(MasterXmlFragment, item, "system.web/roleManager/providers");
                                        }

                                        if (
                                            MasterXmlFragment.SelectSingleNode(
                                                @"system.web/roleManager[@enabled='true']") == null)
                                        {
                                            var roleManagerNode =
                                                (XmlElement)
                                                MasterXmlFragment.SelectSingleNode(@"system.web/roleManager");
                                            roleManagerNode.SetAttribute("enabled", "true");
                                            item["XMLStsConfig"] = MasterXmlFragment.OuterXml;

                                            try
                                            {
                                                item.Update();
                                            }
                                            catch (SPException ex)
                                            {
                                                Logging.LogMessage(950, Logging.LogCategories.STSXML,
                                                                   TraceSeverity.Unexpected,
                                                                   String.Format(
                                                                       "Unable to update the StsFarm List in Central Administration. {0}",
                                                                       ex.StackTrace),
                                                                   new object[] { null });
                                                throw new SPException(
                                                          @"Unable to update the StsFarm List in Central Administration.  Check to see if the item was removed.");
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                SPListItem item = list.Items.Add();
                                item["StsConfig"]    = "MasterXmlFragment";
                                item["XMLStsConfig"] =
                                    @"<system.web><membership><providers /></membership><roleManager enabled='true'><providers /></roleManager></system.web>";

                                try
                                {
                                    item.Update();
                                }
                                catch (SPException ex)
                                {
                                    Logging.LogMessage(950, Logging.LogCategories.STSXML, TraceSeverity.Unexpected,
                                                       String.Format(
                                                           "Unable to update the StsFarm List in Central Administration. {0}",
                                                           ex.StackTrace),
                                                       new object[] { null });
                                    throw new SPException(
                                              @"Unable to update the StsFarm List in Central Administration.  Check to see if the item was removed.");
                                }
                            }
                        }
                    }
                    catch (SPException ex)
                    {
                        Logging.LogMessage(950, Logging.LogCategories.STSXML, TraceSeverity.Unexpected,
                                           String.Format("Unable to update the StsFarm List in Central Administration. {0}",
                                                         ex.StackTrace),
                                           new object[] { null });
                        throw new SPException(@"Unable to update the StsFarm List in Central Administration.  Validate the list exists.");
                    }

                    try
                    {
                        SPList list = site.Lists.TryGetList("Nauplius.ADLDS.FBA - WebApplicationSettings");

                        if (list != null)
                        {
                            SPListItemCollection items = list.Items;

                            foreach (SPListItem item in items)
                            {
                                var zone = GetZone(item);
                                if (item["WebApplicationUrl"].ToString() ==
                                    webApp.GetResponseUri(zone).ToString())
                                {
                                    //Set the Membership and Role providers for the Web Application
                                    var ap = new SPFormsAuthenticationProvider(
                                        item["WebApplicationMembershipProvider"].ToString(), item["WebApplicationRoleProvider"].ToString());

                                    //Set the custom URL
                                    try
                                    {
                                        var customUrl = item["CustomUrl"].ToString();
                                        webApp.IisSettings[zone].ClaimsAuthenticationRedirectionUrl =
                                            new Uri(customUrl, UriKind.RelativeOrAbsolute);
                                    }
                                    catch (NullReferenceException)
                                    {
                                        //CustomUrl is null
                                    }

                                    try
                                    {
                                        webApp.IisSettings[zone].AddClaimsAuthenticationProvider(ap);
                                        webApp.Update();
                                        webApp.ProvisionGlobally();
                                    }
                                    catch (ArgumentException)
                                    {
                                        foreach (
                                            var provider in
                                            webApp.IisSettings[zone].ClaimsAuthenticationProviders)
                                        {
                                            if (provider.ClaimProviderName == "Forms")
                                            {
                                                webApp.IisSettings[zone].DeleteClaimsAuthenticationProvider(provider);
                                                webApp.Update();
                                                break;
                                            }
                                        }
                                        webApp.IisSettings[zone].AddClaimsAuthenticationProvider(ap);
                                        webApp.Update();
                                        webApp.ProvisionGlobally();
                                    }

                                    try
                                    {
                                        WebModifications.CreateWildcardNode(false, webApp, zone);
                                        WebModifications.CreateProviderNode(false, webApp, zone);
                                        WebModifications.CreateStsProviderNode(false, properties, zone);
                                        WebModifications.CreateAdminWildcardNode(false, webApp, zone);
                                        WebModifications.CreateAdminProviderNode(false, webApp, zone);

                                        var local = SPFarm.Local;

                                        var services = from s in local.Services
                                                       where s.Name == "SPTimerV4"
                                                       select s;

                                        var service = services.First();

                                        foreach (SPJobDefinition job in service.JobDefinitions)
                                        {
                                            if (job.Name == tJobName)
                                            {
                                                if (job.IsDisabled)
                                                {
                                                    job.IsDisabled = false;
                                                }
                                                job.Update();
                                                job.RunNow();
                                            }
                                        }
                                    }
                                    catch (SPException ex)
                                    {
                                        Logging.LogMessage(952, Logging.LogCategories.STSXML, TraceSeverity.Unexpected,
                                                           String.Format("An unknown error has occurred. {0}",
                                                                         ex.StackTrace),
                                                           new object[] { null });
                                        throw new SPException(@"An unknown error has occurred. Please review the ULS file.");
                                    }
                                }
                            }
                        }
                    }
                    catch (SPException ex)
                    {
                        Logging.LogMessage(951, Logging.LogCategories.STSXML, TraceSeverity.Unexpected,
                                           String.Format("Unable to update the WebApplicationSettings List in Central Administration. {0}",
                                                         ex.StackTrace),
                                           new object[] { null });
                        throw new SPException(@"Unable to update the WebApplicationSettings List in Central Administration.  Validate the list exists.");
                    }
                }
            }
        }