Wednesday, 2 October 2013

Sitecore: Kick idle or inactive users automatically

Leave a Comment
Sitecore licensing can be based on total number of concurrent user sessions.  When Sitecore client concurrent user sessions limit reaches to its threshold value, next user who wants to login on Sitecore content tree/ desktop will be denied to login. User will get below message because Sitecore license has a concurrent number of users limitation:
There are too many users using the system at this time.
Too many inactive and idle users can cause slowness to Sitecore client and you may also observe performance issues while doing content authoring.  Most of the time users have a habit to exit Sitecore application by simple closing the browser window. Thus Sitecore client never gets a log off command from user manually and user session does not get killed immediately. Since Sitecore server never received a log off command, it waits until the user session time out occurs.  Standard session timeout is 20 minutes.
When managing Client sessions, Sitecore keeps track of every user logged in to the system and assigns a Sitecore user session id to every user accessing a Sitecore Client application. Sitecore Client user session is not limited in time and is kept alive until the browser is opened.
To allow new users to login on Sitecore client, we have to kick inactive and idle user sessions.  Follow below steps to kick inactive and idle user sessions manually:
  1. Login on Sitecore client. Please make sure that either you have login using admin credentials or you have admin privileges.
  2. Navigate to below url:
    http://Sitecore-domain-path/Sitecore/shell/Applications/Login/Users/Kick.aspx
    If you are using Sitecore 8 :
    http://Sitecore-domain-path/sitecore/client/Applications/LicenseOptions/KickUser
  3. You will get list of current user sessions (active and inactive) with login time and last request time.
  4. Select user who is inactive or idle and click on KICK button.
Monitoring active and inactive user session queue can be a tedious task. We can kick Sitecore users if they are inactive or idle for a specified period of time. Using DomainAccessGuard class, we can kick users from Sitecore Programmatically. DomainAccessGuard class allows getting details of all current user sessions. DomainAccessGuard class exists in Sitecore.Web.Authentication namespace.

Following code will kick Sitecore users if they are inactive or idle for a specified period of time:
using System;
using Sitecore.Web.Authentication;
using System.Collections.Generic;

namespace Sitecore
{
    public class KickUserSession
    {
        private TimeSpan maximumIdleTime;

        public KickUserSession(string maximumIdleTime)
        {
            this.maximumIdleTime = TimeSpan.Parse(maximumIdleTime);
        }

        public void Run()
        {
            List<DomainAccessGuard.Session> userSessionList = DomainAccessGuard.Sessions;

            if (userSessionList != null && userSessionList.Count > 0)
            {
                foreach (DomainAccessGuard.Session userSession in userSessionList.ToArray())
                {
                    TimeSpan span = (TimeSpan)(DateTime.Now - userSession.LastRequest);
                    if (span > this.maximumIdleTime)
                    {
                        DomainAccessGuard.Kick(userSession.SessionID);
                        Sitecore.Diagnostics.Log.Audit("Kicked out user is : " + userSession.UserName, this);
                    }
                }
            }
        }
    }
}
Source code and dll can be downloaded from GitHub.
  • Copy Sitecore.KickUserSessionAgent.dll in /bin folder of Sitecore website.
  • Add the following agent into the <scheduling> section in web.config:
<!-- Agent to kick users who are idle for 30 minutes -->
<agent type=" Sitecore.KickUserSessionAgent.KickUserSession" method="Run" interval="00:05:00">
        <param desc="maximumIdleTime">00:30:00</param>
</agent> 
You can configure the maximum inactivity time before a user will be kicked. Please set the appropriate value for the maximumIdleTime parameter.
Comments and suggestions are most welcome. Happy coding!

0 comments :

Post a Comment