Tuesday 27 December 2016

Sitecore - Enable preview mode for Anonymous user


Recently we had a requirement to enable preview mode for anonymous user so that content authors can share the preview url to others for review. Since it was just for review purpose we wanted them to see the preview of the page without logging into sitecore. Of course there is another way to just point CM server database to master but we did not want to change our architecture for this. We contacted Sitecore support(support id 471776) and they helped us in achieving this. Please note the fix is for Sitecore 8.0 version but should work with other version as well.

Configuration- you need to patch your configuration in such a way so that your class will come as below in showconfig page.



<httpRequestBegin>
… … …
<processor type="Sitecore.Pipelines.HttpRequest.UserResolver, Sitecore.Kernel"/>
<processor type="[Your namespace].AllowAnonymousPreview, YourAssemblyName"/>
<processor type="Sitecore.Pipelines.HttpRequest.DatabaseResolver, Sitecore.Kernel"/>
</httpRequestBegin>


Class code :- 

namespace [Namespace]
{
    public class AllowAnonymousPreview : HttpRequestProcessor
    {
        public override void Process(HttpRequestArgs args)
        {
            Assert.ArgumentNotNull(args, "args");
            var activeUser = AuthenticationManager.GetActiveUser();
            Assert.IsNotNull(activeUser, "User cannot be null.");
            var userIsExtranetAnonymous = activeUser.Name == "extranet\\Anonymous";
            var userIsSitecoreAnonymous = activeUser.Name == "sitecore\\Anonymous";
            if (!userIsExtranetAnonymous && !userIsSitecoreAnonymous)
            {
                return;
            }
            var isRibbonRequest = args.Url != null && args.Url.ItemPath == "/sitecore/content/home/applications/webedit/webeditribbon";
            if (Sitecore.Context.PageMode.IsPreview || isRibbonRequest)
            {
                PerformAutoLogin();
            }
         
        }

        private void PerformAutoLogin()
        {
            string userName = "extranet\\Preview Anonymous User";
            AuthenticationManager.Login(userName);
            string ticket = Sitecore.Web.Authentication.TicketManager.CreateTicket(userName, @"/sitecore/shell");
            HttpContext current = HttpContext.Current;
            if (current != null)
            {
                HttpCookie cookie = new HttpCookie(Sitecore.Web.Authentication.TicketManager.CookieName, ticket)
                {
                    HttpOnly = true
                };
                current.Response.AppendCookie(cookie);
            }
        }

    }
}

extranet\\Preview Anonymous User is a custom user which we have created for preview access and have assigned "sitecore\Sitecore Minimal Page Editor" role to him. We have revoked write access for this user and tested and confirmed that user should not be able to go back to edit mode and edit the page with this access.


Happy Coding :)

No comments:

Post a Comment