Skip to content

Instantly share code, notes, and snippets.

@poojarsn
Created March 7, 2018 03:42
Show Gist options
  • Save poojarsn/6bdadefc538b557f8d665ce32bcee391 to your computer and use it in GitHub Desktop.
Save poojarsn/6bdadefc538b557f8d665ce32bcee391 to your computer and use it in GitHub Desktop.
Sitecore Pipeline HttpBeginRequest - Processor HttpRequestProcessor : Intercept requests before page get processed completely.
using Sitecore;
using Sitecore.Pipelines.HttpRequest;
using System.Web.Security;
namespace MyProject.Customm.Pipelines.HttpRequest
{
    /// <summary>
    ///     The pipeline processor intercept site setting EnabledMaintenance flag before request get processed.
    /// </summary>
    public class SiteHandler : HttpRequestProcessor
    {
        private const string _enabeledMaintenancePage = "1";
        public override void Process(HttpRequestArgs args)
        {
        
            if (args != null)
            {
                var siteSettingItem = Context.Database?.GetItem(new Sitecore.Data.ID("{Some Item Id guid}"));
                if (siteSettingItem != null)
                {                  
                    var enabeledMaintenancePage = siteSettingItem["EnableMaintenancePageFieldName"];             
                          
                    // If login page Item shoudl be checked otherwise it will results into - TOO MANY REDIRECTS.
                    if (enabeledMaintenancePage == _enabeledMaintenancePage)
                    {
//If user session is still available.
                        args.Context.Session?.Abandon();
                        args.Context.Session?.Clear();
                        args.Context.Response?.Cookies?.Clear();
                        FormsAuthentication.SignOut();
                        args.Context.Response.Redirect("Maintenance Page URL");
//Abort pipeline to prevent: Too many redirects.
                        args.AbortPipeline();
                        return;
                    }
                }
            }
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment