0

I am on Sitecore 9 update 1 , XP1 topology-Azure PaaS environment.

I am looking for an option of redirecting any Content Management URL to Experience Editor mode. It will redirect login if not logged in.

My CM instance has to be available over internet so I would like to have this flexibility.

I have an idea of doing it by "rewrite" under web.config by prepending sc_mode=edit

    <rule name="Add sc mode edit" stopProcessing="true">
      <match url=".*" />
        <conditions>
          <add input="{QUERY_STRING}" pattern="sc_mode=edit" negate="true" />
          <add input="&amp;{QUERY_STRING}" pattern="^(&amp;.+)|^&amp;$" />
        </conditions>
      <action type="Redirect" url="{R:0}?sc_mode=edit{C:1}" appendQueryString="false"  />
    </rule>

I already have below rewrite code in my web.config

<rewrite>
  <rules>
    <rule name="Root Hit Force HTTPS Redirection" enabled="true" stopProcessing="true">
      <match url="^$" ignoreCase="false" />
      <conditions>
        <add input="{HTTPS}" pattern="^OFF$" />
        <add input="{HTTP_METHOD}" pattern="GET" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}/" redirectType="Permanent" />
    </rule>
    <rule name="Sitecore Login or Admin Force HTTPS Redirection" enabled="true" stopProcessing="true">
      <match url="^(sitecore/(login|admin))$" />
      <conditions>
        <add input="{HTTPS}" pattern="^OFF$" />
        <add input="{HTTP_METHOD}" pattern="GET" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
    </rule>
    <rule name="Forbidden" enabled="true" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTPS}" pattern="^OFF$" />
        <add input="{HTTP_METHOD}" pattern="GET" />
      </conditions>
      <action type="CustomResponse" statusCode="403" subStatusCode="0" />
    </rule>        
  </rules>
</rewrite>

Combining both results in two rule for ".*" match ; and givs error in edit mode as "severe error contact Administrator" and none of the button or link click is working only above error just below Editor ribbon is coming.

Need suggestion on how to proceed on this or take some other route to achieve this.

1 Answer 1

1

You can force the PageMode with a pipeline processor to set the display mode, this will cause the user to be redirected to the Sitecore Login page if they are not previously logged in:

public class SetPageMode : HttpRequestProcessor
{
  public override void Process(HttpRequestArgs args)
  {
    if (Sitecore.Context.PageMode.IsNormal)
    {
      Sitecore.Context.Site.SetDisplayMode(DisplayMode.Edit, DisplayModeDuration.Remember);
    }
  }
}

This will force any regular URL to edit mode as well (e.g. /path-to/custom/page), even those without an sc_mode parameter set, which can be useful if you email a link to a user.

You will also need to add another pipeline processor to redirect the user back to the original page after login.

public class LoginRedirect : LoggedInProcessor
{
  public override void Process(LoggedInArgs args)
  {
    if (Sitecore.Context.GetSiteName() != "login") { return; }

    string url = HttpUtility.UrlDecode(WebUtil.GetQueryString("url", ""));
    if (!string.IsNullOrWhiteSpace(url))
    {
      WebUtil.Redirect(url);
    }
  }
}

And then patch these in, also setting Authentication.SaveRawUrl=true so the url the user was trying to view is also passed through:

<configuration 
    xmlns:patch="http://www.sitecore.net/xmlconfig/" 
    xmlns:set="http://www.sitecore.net/xmlconfig/set/"
    xmlns:role="http://www.sitecore.net/xmlconfig/role/">
  <sitecore role:require="ContentManagement">

    <settings>
      <setting name="Authentication.SaveRawUrl" set:value="true"/>
    </settings>

    <pipelines>
      <httpRequestBegin>
        <processor type="Custom.Project.Pipelines.SetPageMode, Custom.Project"
                   patch:after="*[@type='Sitecore.Pipelines.HttpRequest.SiteResolver, Sitecore.Kernel']" />
      </httpRequestBegin>
    </pipelines>

    <processors>
      <loggedin>
        <processor mode="on" type="Custom.Project.Processors.LoginRedirect, Custom.Project" />
      </loggedin>
    </processors>

  </sitecore>
</configuration>

Only apply this config patch to your CM instance (i.e. do not deploy to your CD servers), but since you are using Sitecore 9, you should use the config roles feature for easier management. The above example config has the ContentManagement config role set.

I recommend only switching the mode if the request mode is normal (which is what the code above is doing), this will allow your content authors to continue to use the preview, debug and explorer modes.

Your Answer

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

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