:::: MENU ::::

Thursday, April 28, 2011

You can create an Automatic Logout user control by doing the following.

1. Create an automatic logout user control.
2. Add 1 RadAjaxPanel, 2 RadAjaxTimers (within the Panel), and 1 RadWindowManager.  Your ASPX will look something like this...

1

<radA:RadAjaxPanel ID="RadAjaxPanel1" runat="server" Height="1px" Width="1px">  

2

      <radA:RadAjaxTimer ID="RadAjaxTimer1" runat="server" AutoStart="False" /> 

3

      <radA:RadAjaxTimer ID="RadAjaxTimer2" runat="server" AutoStart="False" /> 

4

</radA:RadAjaxPanel> 

5

<radW:RadWindowManager ID="TimeoutWarningPopup" runat="server">  

6

</radW:RadWindowManager> 


3. Create a timeout message ASPX page.  This page will display 10 seconds before the user is logged out.  Mine says, "You will be logged out in 10 seconds."
4. In the user control code behind add the following code...

Imports Telerik.WebControls  

 

Partial Class includes_uc_telerik_ucAjaxTimer  

    Inherits System.Web.UI.UserControl

#Region "-- Members --"  

    Private m_blnTickEvent As Boolean = False

#End Region  

 

#Region "-- Properties --" 

    Public Property TickEvent() As Boolean 

        Get 

            Return m_blnTickEvent  

        End Get 

        Set(ByVal Value As Boolean)  

            m_blnTickEvent = Value  

        End Set 

    End Property

#End Region  

 

    Protected Sub RadAjaxTimer1_Tick(ByVal sender As ObjectByVal e As Telerik.WebControls.TickEventArgs) Handles RadAjaxTimer1.Tick  

        RadAjaxPanel1.ResponseScripts.Add("radopen'../../web/telerik/ajax_timer_msg.aspx','RadWindow1');")  

        RadAjaxTimer1.Stop()  

        Me.TickEvent = True 

    End Sub 

 

    Protected Sub RadAjaxTimer2_Tick(ByVal sender As ObjectByVal e As Telerik.WebControls.TickEventArgs) Handles RadAjaxTimer2.Tick  

        Response.Redirect("../../web/security/login.aspx")  

    End Sub 

 

    Protected Sub Page_PreRender(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.PreRender  

        If Me.TickEvent = False Then 

           RadAjaxTimer1.Interval = 3600000 '1 hour  

           RadAjaxTimer1.Start()  

 

           RadAjaxTimer2.Interval = 3600000 + 12000 '1 hour + 12 seconds  

           RadAjaxTimer2.Start()  

        End If 

    End Sub 

      

End Class 

 


5. Place the user control inside a master page or any pages that needs a logout timer.

Okay...  Let's break this down.

When the master page loads the user control Page_PreRender event will fire during the order of operations.  The Page_PreRender will start both timers only if the TickEvent property is False (default).  The property is needed becuase the Page_PreRender will fire after the RadAjaxTimer1_Tick is fired - we don't want to restart the timer on this event.

After Timer1 interval is reached the RadAjaxTimer1_Tick event is fired.  This event opens the message window stating you will be logged out, and sets the TickEvent property (see above for the reason why).  

If the user does nothing...  RadAjaxTimer2_Tick event fires and the user is sent to the login screen.

If the user causes a postback (clicks a link, button, etc.) the timers start again and keep the user logged in.

I have set my SessionTimeout to 61 minutes.  The session also resets when the user causes a postback so I think I'm safe in both cases.

So...  If a user walk away from the machine for an hour or has an extended period of inactivity...  They get logged out.

I hope this helps someone that might need the same functionality.