Surendra Sharma

Surendra Sharma

Search This Blog

Thursday, March 23, 2017

How to expire Sitecore session forcefully



In one of our project, I have to forcefully expire the Sitecore session so that it flush the data to MongoDB.

For this either you can restart IIS or start-stop your website. But this action require you to login into the system. But what if you are accessing Sitecore remotely or over the internet.

So I write this script which expire the session and clear the Sitecore analytics related cookies.

<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <%
            Context.Session.Abandon();
            HttpContext.Current.Request.Cookies.Remove("ASP.NET_SessionId");
            Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", string.Empty));
            HttpContext.Current.Request.Cookies.Remove("SC_ANALYTICS_SESSION_COOKIE");
            Response.Cookies.Add(new HttpCookie("SC_ANALYTICS_SESSION_COOKIE", string.Empty));
            HttpContext.Current.Request.Cookies.Remove("SC_ANALYTICS_GLOBAL_COOKIE");
            Response.Cookies.Add(new HttpCookie("SC_ANALYTICS_GLOBAL_COOKIE", string.Empty));
        %>
    </form>
</body>
</html>

I called this script as "ForceSessionEnd.aspx" and keep it in "Website" folder.

Access this script over the browser as "http://yourinstancename/forcesessionend.aspx".

After running this page, you will get latest data on mongo database.

Feel free to add more cookies as per your need.

I hope you like this Sitecore script. Stay tuned for more Sitecore related articles.

Till that happy Sitecoring :)
 
Please leave your comments or share this script if it’s useful for you.

No comments:

Post a Comment