Andreas Gehrke

Disable Sitecore filters for Web API controllers

I have previously written about how easy it to implement Web API controllers in Sitecore. However since Sitecore adds a lot of global filters for authorization and security policies - e.g. see sitecore/api/configuration/filters section in Sitecore.Services.Client.config. You might want to disable these filters, as they can interfere when building a public accessible API - see the code below. During a debugging session I noticed that a filter from some FXM assembly were also added.

To be fair, most of Sitecore's implementations of IFilter/ActionFilterAttribute that are assigned globally on HttpConfiguration, does check for presence of ServicesControllerAttribute and inheritance of ServicesApiController. This means you might prevent these filters from doing anything by using System.Web.Http.ApiController as base class for your controllers. I haven't been though the code for all Sitecore's filters, so I'm playing it safe, and decorate my ApiControllers with a ClearSitecoreWebApiConfigAttribute.

ApiController.cs:

/// <summary>
/// Available at url: /sitecore/api/ssc/example1/andreas/{id}/{action}
/// </summary>
[Sitecore.Services.Core.ServicesController("example1.andreas")]
[ClearSitecoreWebApiConfig]
public class MyController : System.Web.Http.ApiController
{
	[HttpGet]
	public IHttpActionResult MyAction()
	{
		// Implement
	}
}

ClearSitecoreWebApiConfigAttribute.cs:

using System;
using System.Linq;
using System.Web.Http;

namespace YourNamespace
{
    /// <summary>
    /// Clear Sitecore's Web API config for a <see cref="ApiController"/>.
    /// </summary>
    /// <remarks>
    /// Removes <see cref="System.Web.Http.Filters.IFilter"/> and <see cref="System.Net.Http.Formatting.MediaTypeFormatter"/> set by Sitecore.
    /// Note that this means Authorization Filters and Security Policies defined by Sitecore.Services.Client will no longer work for this controller.
    /// </remarks>
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
    public class ClearSitecoreWebApiConfigAttribute : Attribute, System.Web.Http.Controllers.IControllerConfiguration
    {
        public void Initialize(System.Web.Http.Controllers.HttpControllerSettings controllerSettings, System.Web.Http.Controllers.HttpControllerDescriptor controllerDescriptor)
        {
            // Remove Sitecore's Sitecore.Services.Infrastructure.Web.Http.Formatting.BrowserJsonFormatter configured in
            // Sitecore.Services.Infrastructure.Sitecore.ApplicationContainer.ResolveServicesWebApiConfiguration().
            var sitecoreFormatter = controllerSettings.Formatters.FirstOrDefault(formatter => formatter.GetType() == typeof(Sitecore.Services.Infrastructure.Web.Http.Formatting.BrowserJsonFormatter));
            if (sitecoreFormatter != null) controllerSettings.Formatters.Remove(sitecoreFormatter);

            // Remove the ConfigurationFilterProvider as this is responsible for providing the globally configured Sitecore filters
            var filterProviders = controllerSettings.Services.GetFilterProviders();
            var configurationFilterProvider = filterProviders.FirstOrDefault(provider => provider.GetType() == typeof(System.Web.Http.Filters.ConfigurationFilterProvider));
            if (configurationFilterProvider != null)
            {
                controllerSettings.Services.Remove(typeof(System.Web.Http.Filters.IFilterProvider), configurationFilterProvider);
            }
        }
    }
}

I encourage the developers of Sitecore to consider using controller-specific configuration instead of loading the global HttpConfiguration with all kinds of module or feature specific stuff.

Tags: #dotnet-dev #sitecore