0

we are trying to access the session variables on session end. We have created a new module called ApplicationSessionEventsHandlerModule and added a SessionEnd_Execute handler.

However, the Kentico SessionHelper class does not work and neither does System.Web.HttpContext.Current.Session. How do you recommend we access session variables on session end? See code:

  public class ApplicationSessionEventsHandlerModule : Module
    {
    public ApplicationSessionEventsHandlerModule() :
        base("ApplicationSessionEventsHandlerModule")
        {
        }

    protected override void OnInit()
        {
        base.OnInit();
        ApplicationEvents.SessionStart.Execute += SessionStart_Execute;
        ApplicationEvents.SessionEnd.Execute += SessionEnd_Execute;
        }

    private void SessionEnd_Execute(object sender, EventArgs e)
        {

        if (SessionHelper.SessionIsAvailable)
            {
            if (SessionHelper.GetValue("UserDetails") != null)
                {
               //Do stuff
                    }
                }

            }


    private void SessionStart_Execute(object sender, EventArgs e)
        {

        if (SessionHelper.GetValue("UserDetails") != null)
            {
          //do stuff
            }
        }
    }

1 Answer 1

0

Reference link - https://docs.xperience.io/k11/custom-development/handling-global-events/reference-global-system-events#ReferenceGlobalsystemevents-ApplicationEvents

Please try the following -

  1. Create a Global folder under \App_Code\

  2. Create a ApplicationSessionEventsHandler.cs file under Global folder with the following code snippet and try -

     using CMS.Base;
     using CMS.DocumentEngine;
     using CMS.Helpers;
     using CMS.Membership;
    
     [ApplicationSessionEventsHandler]
     public partial class CMSModuleLoader
     {
     public class ApplicationSessionEventsHandler : CMSLoaderAttribute
     {
     public override void Init()
     {
     ApplicationEvents.SessionEnd.Execute += SessionEnd_Execute;
     ApplicationEvents.SessionStart.Execute += SessionStart_Execute;
    
     }
    
     private void SessionStart_Execute(object sender, System.EventArgs e)
     {
     if (SessionHelper.GetValue("UserDetails") != null)
     {
     //do stuff
     }
     }
     private void SessionEnd_Execute(object sender, System.EventArgs e)
     {
     if (SessionHelper.SessionIsAvailable)
     {
     if (SessionHelper.GetValue("UserDetails") != null)
     {
     //Do stuff
     }
     }
     }
     }
     }
    
1
  • I tried this but it seems as though the CMSLoaderAttribute class does not exist. Is there an updated solution for v11? Feb 15, 2022 at 18:56

Your Answer

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

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