1

Im doing below in mvc.requestbegin processor

if (!IdentityChecker.IsUserIdentityValid(args.RequestContext.HttpContext.Request.RequestContext.HttpContext.Request) && !args.RequestContext.HttpContext.Response.IsRequestBeingRedirected)
{
 args.AbortPipeline(); --// step1 
 args.RequestContext.HttpContext.Response.RedirectPermanent("/LogOn/SignIn", true); -- step2
     }

Scenario: on browsing context item as "Home" and on above condition being true(user is not authenitcated) the page is redirected to "Logon/signin", but the Sitecore request processing for "Home" item does not end, it logs below exception:

Exception: Sitecore.Mvc.Diagnostics.ControllerCreationException
Message: Could not create controller: 'Sitecore'. 
Source: Sitecore.Mvc
   at Sitecore.Mvc.Controllers.SitecoreControllerFactory.CreateController(RequestContext requestContext, String controllerName)
   at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory)
   at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step)
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Nested Exception

Exception: System.InvalidOperationException
Message: Attempt to retrieve context object of type 'Sitecore.Mvc.Presentation.PageContext' from empty stack.
Source: Sitecore.Mvc
   at Sitecore.Mvc.Common.ContextService.Peek[T]()
   at Sitecore.Mvc.Pipelines.MvcPipelineArgs.get_PageContext()
   at Sitecore.Mvc.Pipelines.Request.CreateController.CreateItemController.CreateController(CreateControllerArgs args)
   at Sitecore.Mvc.Pipelines.Request.CreateController.CreateItemController.Process(CreateControllerArgs args)
   at (Object , Object[] )
   at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
   at Sitecore.Mvc.Pipelines.PipelineService.RunPipeline[TArgs](String pipelineName, TArgs args)
   at Sitecore.Mvc.Pipelines.PipelineService.RunPipeline[TArgs,TResult](String pipelineName, TArgs args, Func`2 resultGetter)
   at Sitecore.Mvc.Controllers.SitecoreControllerFactory.CreateSitecoreController(RequestContext requestContext, String controllerName)
   at Sitecore.Mvc.Controllers.SitecoreControllerFactory.CreateController(RequestContext requestContext, String controllerName)

how should i code so that sitecore item processing for "Home" items ends, on redirection to another page and does not log any exception.

mvcrequestbegin processor code:

namespace Portal.Code.Pipelines
{
    using System;
    using System.Diagnostics.CodeAnalysis;
    using LogOn.Authenticator;
    using Sitecore.Mvc.Pipelines.Request.RequestBegin;

    public class MvcRequestBeginProcessor : Sitecore.Mvc.Pipelines.Request.RequestBegin.RequestBeginProcessor
    {
        public override void Process(RequestBeginArgs args)
        {
            if (
                    !IdentityChecker.IsUserIdentityValid(
                        args.RequestContext.HttpContext.Request.RequestContext.HttpContext.Request) &&
                        !args.RequestContext.HttpContext.Response.IsRequestBeingRedirected)
                {
                    args.AbortPipeline();
                    args.RequestContext.HttpContext.Response.Redirect("/LogOn/SignIn", true);
                }

        }
    }
}
2
  • 1
    Can you update your question with code of your whole processor? Dec 19, 2018 at 6:11
  • 1
    @PeterProcházka - added the processor code.
    – nari447
    Dec 19, 2018 at 9:21

2 Answers 2

3

The preferred way to handle login in Sitecore is to deny anonymous access to your items, and set up a login page in your site definition like this:

   <site name="website"
       rootPath="/sitecore/content/"
       startItem="/Home"
       loginPage="/LogOn/SignIn"
    </site>

Now Sitecore will take care of the redirection to the login page for you when a user does not have access. If you still want to handle the redirection manually, try using the httpRequestBegin pipleline, and use the HttpContext directly from the System.Web namespace like this

args.AbortPipeline();
System.Web.HttpContext.Current.Response.Redirect("/LogOn/SignIn", true)

If you have no other options, and must do this in the mvc.requestBegin, try the following:
Let the SetupPageContext processor do his job first, so the context is created. To achieve this, try patching in your processor right after the SetupPageContext processor. Now you should get rid of the error message in your log file.

2
  • I could not move this logic to RequestBegin processor, as it requires more rfactoring of existing, which i dont have time for. however https://community.sitecore.net/developers/f/8/p/8985/19552#19552 suggests that sitecore logs this exception when Args.AbortPipeline is used in MVC.RequestBegin pipeline. is there any workaround to not log this exception?
    – nari447
    Nov 20, 2018 at 16:16
  • I second using the permissions to drive the redirect/access, but I would probably leverage the Sitecore.Web.WebUtil.RedirectToLoginPage();. It is basically the same under the hood but is aware of the loginPage attribute.
    – vandsh
    Nov 21, 2018 at 16:04
0

I've fixe this by changing the code from

if (!IdentityChecker.IsUserIdentityValid(args.RequestContext.HttpContext.Request.RequestContext.HttpContext.Request) && !args.RequestContext.HttpContext.Response.IsRequestBeingRedirected)
{
 args.AbortPipeline(); --// step1 
 args.RequestContext.HttpContext.Response.RedirectPermanent("/LogOn/SignIn", true); -- step2
     }

to

if (!IdentityChecker.IsUserIdentityValid(args.RequestContext.HttpContext.Request.RequestContext.HttpContext.Request) && !args.RequestContext.HttpContext.Response.IsRequestBeingRedirected)
{
 Sitecore.Context.Item = Sitecore.Context.Database.GetItem(ItemIds.ServerErrorPageId); --// step1 
 args.RequestContext.HttpContext.Response.RedirectPermanent("/LogOn/SignIn", true); -- step2
     }

Just changing the context item to some default item in case if user is not logged in, so that Sitecore always finds page context and wont try to instantiate any controllers that are bound to functionality.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.