Sitecore Pipeline Processor With HttpContext.Current.Session

For one of the projects I’m leading, I need to access HttpContext.Current.Session on every page request.  Long story short, I have a very custom implementation of a “membership provider” and like any site with authentication, there are pages that are only accessible to users if they are logged in.  This custom membership provider stores user information in Session and so, on every page request, I need to access Session and see if certain properties of the user were already set, in other words, was the user logged in.


I started off with trying the httpRequestBegin pipeline.  To my surprise, by the time this pipeline runs, HttpContext.Current is set but not HttpContext.Current.Session.  I went to Google and searched for “sitecore pipeline session”.  First result – a question by Dan Solovay on StackOverflow, which he answers himself!  🙂

Not sure what I was doing wrong but for me, this solution only ran once when the app started.  I couldn’t get Dan’s solution to run on every page load.


Searching some more on Google, I found this post by briancaos where he suggests using the httpRequestProcessed pipeline.  This pipeline does run on every page request, however, it runs after all renderings are processed which I didn’t want.  I wanted to run my login check after the item was resolved but before any renderings were processed.


I decided to look at all of the different pipelines that are available so I went to my /sitecore/admin/showconfig.aspx page, searched for <pipelines> and then just scrolled down skimming through each one.  That’s when I found renderLayout.  I thought, “renderLayout must run first before all my renderings.”  Not the case!  renderLayout runs after all of your renderings.  I think the way it works is, first Sitecore processes each rendering individually.  Once it has all of that HTML, it renders your Layout with the HTML from each rendering in the appropriate placeholders.  (Don’t quote me on that.  I’m only basing this on how my breakpoints were hit by the debugger in Visual Studio).


So when renderLayout proved to be too late for my purposes, I thought, “maybe I can intercept the first field being rendered and do my checks there”.  So I tried the renderField pipeline.  This pipeline, does, in fact, run before renderLayout so that was good.  And Session is set in this pipeline.  Also good.  The problem is, Sitecore seems to kick-off this pipeline multiple times at once and so I was literally getting a StackOverflow exception.  Not exactly sure what was causing the StackOverflow exception and unfortunately, I didn’t really have time to investigate it further.  So renderField wasn’t going to work either.  Now what?!?!?!


After trying all of these pipelines, I decided to ask this question on the Sitecore Slack chat room.  By the way, if you’re not in the Sitecore Slack chat room, you really should join.  There are a lot of people there with extensive knowledge of Sitecore more than willing to help you.  Here’s the URL,  Check it out.  Only thing is, I think you need to be invited.  If you try joining and can’t, leave a comment here and I’ll ask one of the Administrators to invite you.

Getting back to the topic at hand.  While discussing this issue with other developers in the chat room, Mark Cassidy gave me a suggestion that seems to be the solution I needed!  He suggested I try adding my processor to the mvc.beginRequest pipeline and turns out, this pipeline does, in fact, cover all of my requirements – runs before any renderings are processed but after HttpContext.Current.Session has been set.  There’s only one caveat to this solution – you must be using MVC with Sitecore, not Webforms as this pipeline only runs for MVC requests.  This was not a problem for me since I am using MVC.

So here’s a code example of a pipeline processor for the mvc.beginRequest pipeline that checks if Session is ready to be used:

using System.Web;
using Sitecore.Mvc.Pipelines.Request.RequestBegin;

namespace MySolution.Pipelines.MvcRequestBeginProcessors
    public class MyMvcRequestBeginProcessor : RequestBeginProcessor
        public override void Process(RequestBeginArgs args)
            if (HttpContext.Current != null && HttpContext.Current.Session != null)
                // do something with HttpContext.Current.Session

And here’s how you’d patch this into the Sitecore configs.  As I mentioned before, I wanted this to run before any rendering and my layout so I patched it in before the first processor in this pipeline.

<configuration xmlns:patch="">
                <processor type="MySolution.Pipelines.MvcRequestBeginProcessors.MyMvcRequestBeginProcessor, MySolution"
                    patch:before="processor[@type='Sitecore.Mvc.Pipelines.Request.RequestBegin.SetupPageContext, Sitecore.Mvc']"/>


2 thoughts on “Sitecore Pipeline Processor With HttpContext.Current.Session

    • Doug on

      Hi Santosh. Thanks for the feedback!

      For this project, I am not using Virtual Users. I know that is a very common way of handling permissions and such for extranet users in Sitecore but this implementation requires that most things related to permissions for a user be handled outside of Sitecore.

      Thanks again!

Leave a Reply