Different ways of Storing Page URL while session expires or User Logout

Hello everyone, In this blog, we will discuss few ways to store last access URL for the users so that we can provide seamless login and same journey for the user. 

So recently we got same kind of requirement, where we wanted user to get the last access page if session expires or user is logged out mistakenly.

We started exploring different ways.

First thing came in mind was to use query string parameter to pass the last access web URL of the page. This works absolutely fine but there are situation where the web page URL can have appended query string parameters. So it will be difficult to pass URL with query string as parameter to login page URL. Also if there are security mechanism like WAF implemented on your website then, there is a high chance it will not allow query string to pass from non-authenticated pages to your controller. This was the same scenario which we were facing. Somehow, the query string was removed automatically while the user was redirecting to login page whenever session expired or user was logged out.

Then again, we started looking for alternate solution like caching the URL on server side but that also didn't worked properly for us. Session is also not useful in such cases.

After doing a little bit of more research we found a way of storing the last access web page URL of the user at server side using Sitecore inbuilt property only.

The code was very simple and it is pasted below. First snippet is used to store the last access URL. We can store the data in Sitecore.Context.ClientData property before redirecting the user to login page. The lastAccessUrl is the key which will be used later for getting the URL back

       
       #To set the value in Sitecore ClientData property
       	Sitecore.Context.ClientData.SetValue("lastAccesurl", redirecturlafterlogin);
	WebUtil.Redirect(login url, false);
	   
 

The second snippet is for reading the last access URL and making it empty once you get the data. This is important because it will always persist your last access url eventhough user purposely logout from the sytem.

       
        #To get the value from Sitecore ClientData property
       	string savedQs = StringUtil.GetString(Sitecore.Context.ClientData.GetValue("lastAccesurl"), "");
        
        #To remove last access url once fetched
        Sitecore.Context.ClientData.RemoveValue("lastAccesurl");
	   
 

Thanks for reading and Happy learning.

Comments

Popular posts from this blog

Sitecore XM Cloud Form Integration with Azure Function as Webhook

Automate RSS Feed to Sitecore XM Cloud: Logic App, Next.js API & Authoring API Integration

Create and Fetch Content From Sitecore Content Hub One using GraphQL and React