Click here to Skip to main content
15,885,936 members
Articles / Web Development / ASP.NET

Handle Session Timeouts on DotNetNuke Through an HttpModule

Rate me:
Please Sign up or sign in to vote.
4.00/5 (8 votes)
15 Jul 2008CPOL5 min read 288.4K   500   30  
This article provides an HttpModule that tackles the session-timeout problem on DotNetNuke (DNN) platform. It will redirect users’ subsequent requests to a page that prompts some informative messages after session timeouts occur.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DotNetNuke.Common;
using DotNetNuke.Entities.Tabs;
using DotNetNuke.Entities.Portals;

/// <summary>
/// Summary description for SessionTimeoutModule
/// </summary>
public class SessionTimeoutModule:IHttpModule
{
    private HttpApplication app;

    public void Init(HttpApplication context)
    {
        app = context;
        app.PreRequestHandlerExecute += new EventHandler(app_PreRequestHandlerExecute);
    }

    void app_PreRequestHandlerExecute(object sender, EventArgs e)
    {


        if(app.Context.Session != null) 
        {
            if (app.Context.Session.IsNewSession) 
            {
                string szCookieHeader = app.Context.Request.Headers["Cookie"];

                if (szCookieHeader != null && szCookieHeader.IndexOf("ASP.NET_SessionId") >= 0) 
                {
                    int tabId = int.Parse(app.Context.Request.QueryString["TabId"]);
                    TabController tabController = new TabController();
                    PortalSettings portalSettings = (PortalSettings)app.Context.Items["PortalSettings"];
                    TabInfo tabInfo = tabController.GetTab(tabId, portalSettings.PortalId, true);

                    if (tabInfo.TabName != "SessionTimeout")
                    {
                         tabInfo = tabController.GetTabByName("SessionTimeout", portalSettings.PortalId);
                        string pageUrl = Globals.NavigateURL(tabInfo.TabID);

                        app.Context.Response.Redirect(pageUrl);
                    }
                }
            }
        }
    }

    public void Dispose()
    {

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer ITCAC(數位協進)
Taiwan Taiwan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions