How to redirect to an external URL on clicking Sitecore Logout Button

Scenario:

When you are logged into Sitecore and then click the "Log out" link, you are usually redirected to the login page. Instead, redirect to an external url.


Solution: Intercept the logout pipeline and add the necessary redirect url just before processing the GotoLogin processor.

As to how you find the pipeline/processor to change, it is more related to some search and analysis skills with Showconfig.aspx!

Step-by-Step:

1. Create a class library, I named it SitecoreLogout and add the Process method as follows:

public void Process(LogoutArgs args)

        {

            Tracer.Info("Intercepting call to Sitecore logout. Redirecting to Single Logout instead");

            Sitecore.Text.UrlString urlString = new Sitecore.Text.UrlString("https://www.google.com");

            args.RedirectUrl = urlString;

        }


Note that this is how processors within the (logout) pipeline are written. If you want to get an idea, dotpeek existing methods in Sitecore.Kernel.dll. When I dotpeek GotoLogin, this is what I see:


2. Taking one step backward, if you look at Showconfig.aspx, you can see GotoLogin pipeline processor:

As to how I decided this is the place that needs to be changed, its more of your search skills at use. Since we want to customize the logout link, I searched for "logout". Note that Sitecore naming convention is intuitive so, if you search about what you are up to, you should get some clues.

3. Note that ClearCache, CheckModified, ClearSession, RemoveTicket, GotoLogin and ClearDisplayMode all will share the same Process method signature of passing the LogoutArgs param. You can confirm this again through dotpeek. 



The LogoutArgs param is used to pass values from one processor to another within this Logout pipeline.  Remember this concept whenever you use processors and pipeline hereon. So, this is the concept we are going to use in our current situation too. And, that is why we have the code in Step 1 within a custom class library created for this purpose.

4. Also, in the concerned code, the most important line of code is assignment of RedirectUrl since this will be used by the GotoLogin processor and we will patch our custom assembly method just before the GotoLogin processor.

5. In order to create  a patch config file, create one like this, I named this file  as Sitecore.LogoutRedirect.config -

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <speak.logout>
        <processor type="SitecoreLogout.LogoutRedirectProcessor" patch:before="processor[@type='Sitecore.Pipelines.Logout.GotoLogin, Sitecore.Kernel']"/>
      </speak.logout>
    </pipelines>
  </sitecore>
</configuration>

The most important parts in this config code are : 
a. the pipeline name, which is speak.logout , I initially named it logout and saw that the custom code wasn't executed but later figured out (from elsewhere in the showconfig.aspx) it must be speak.logout 
b. processor type is the <namespace>.<classname> of the custom assembly method we wrote
c. Patch before is the info about the method before which we need to add our logic

Project structure:



6. Now, build the dll and publish it. I manually copied over the dll to my bin folder since its a single dll. I then manually created zzz folder under App_config folder of my site root (screen shot below). You can use your .net project publish skills to do these.



7. Do a IISReset to be sure your changes take effect. 

8. When you open the sitecore url, and press logout, the user should be redirected to google.com since GotoLogin method is assigned a new redirect url by the custom method we created. 

Ensure you pass the external website name through a configuration instead of hard-coding in the custom method.

9. Finally, when you open showconfig.aspx and search for LogoutRedirectProcessor, do not be disappointed to not find your processor just above the processor you patched in -



10. But, it should definitely be present with details of the config -


11. To confirm that the processor executes at the right point, if you build the assembly dll with debug flag and attach the website process, the debug point should be hit. Understandably, the debug point won't be hit if the latest code changes are not compiled into the debug dll.

Sample code here


Comments

  1. I tried this, looks pretty cool. But when I clicked 'Logout' from content editor or any other screen, my custom pipeline is not executing. Only if I clicked on Experience Platform screen the Pipeline is executing and redirection is happening. Is there any way I could over come from this

    ReplyDelete
    Replies
    1. Hi ! Have you found the answer ? I have same problem =_=

      Delete

Post a Comment