Solved

The code to check if Session has expired-- C#.Net

Posted on 2012-03-22
3
1,615 Views
Last Modified: 2012-04-25
I have Login.aspx page in my web-application. When the session expires (session timeout=15) the user will be redirected to the Login.aspx page.

The Orders.aspx page contains Logout link. When the user clicks the Logout link..the Login.aspx page shows up.

I need to create some logic to bring up a Modal window in the center of the screen After Login.aspx was displayed to the user..in other words

if the Login page was displayed to the User as result of SessionTimeout ..the modal window should be displayed in the middle of the screen;  this window will show a Message "Session has expired" to the User  and OK button to close this moda window. So there are Two Possible scenarios:


1) If the User hits Logout link..on the  Orders.aspx page...the Login.aspx page will be displayed; nothing else is required in such case.


2) If Session has Expired..the Login.aspx page will be displayed to User; after that a modal window (showing "Session has expired") pop-ups in the

middle of the screen; The user hits OK button to close this window.

There is a good info on the web showing how to create aspnet-session-timeout control. The url:

http://www.eggheadcafe.com/tutorials/asp-net/7262426f-3c65-4c90-b49c-106470f1d22a/build-an-aspnet-session-timeout-redirect-control.aspx


Can I use the below logic in my Login.aspx (from the above link) to develop the above functionality (#1 And #2)?? Is it OK to use onPrerender(..)  event?
 What can I change to improve this logic?


------ mY Logic in Login.aspx Page-----

 protected override void OnPreRender(EventArgs e)
        {
            // base.OnPreRender(e);

   
            if (Context.Session != null)
            {
                 if (Context.Session.IsNewSession)
                {
                    string sCookieHeader = Page.Request.Headers["Cookie"];
                     if ((null != sCookieHeader) && (sCookieHeader.IndexOf("ASP.NET_SessionId") >= 0))
                     {
                          if (Page.Request.IsAuthenticated)
                          {
                              // FormsAuthentication.SignOut();
                          }
                          // Page.Response.Redirect(this._redirectUrl);

                          lblLabel1.Text="Session has expired"; // later add code to display pop-up window instead of label lblLabel1
                    }
                }
            }
        }

------

The original code from web-site: It also allows to download a sample project.

namespace PAB.WebControls
{
    using System;
    using System.ComponentModel;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
 
    [DefaultProperty("Text"),
        ToolboxData("<{0}:SessionTimeoutControl runat=server></{0}:SessionTimeoutControl>")]
    public class SessionTimeoutControl : Control
    {
         private string _redirectUrl;
 
        [Bindable(true),
            Category("Appearance"),
            DefaultValue("")]
        public string RedirectUrl
        {
            get { return _redirectUrl; }
 
            set { _redirectUrl = value; }
        }
 
 
        public override bool Visible
        {
            get { return true; }
 
 
        }
 
 
        public override bool EnableViewState
        {
            get { return false; }
        }
 
 
        protected override void Render(HtmlTextWriter writer)
        {
            if (HttpContext.Current == null)
                 writer.Write("[ *** SessionTimeout: " + this.ID + " *** ]");
            base.Render(writer);
        }
 
 
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
             if (this._redirectUrl == null)
                throw new InvalidOperationException("RedirectUrl Property Not Set.");
            if (Context.Session != null)
            {
                 if (Context.Session.IsNewSession)
                {
                    string sCookieHeader = Page.Request.Headers["Cookie"];
                     if ((null != sCookieHeader) && (sCookieHeader.IndexOf("ASP.NET_SessionId") >= 0))
                     {
                          if (Page.Request.IsAuthenticated)
                          {
                              FormsAuthentication.SignOut();
                         }
                          Page.Response.Redirect(this._redirectUrl);
                    }
                }
            }
        }
    }
}

Thanks
0
Comment
Question by:niceguy971
3 Comments
 
LVL 7

Assisted Solution

by:gopaltayde
gopaltayde earned 250 total points
ID: 37756110
0
 
LVL 9

Accepted Solution

by:
gery128 earned 250 total points
ID: 37757199
modify your code like this:

if (Page.Request.IsAuthenticated)
                          {
                              // FormsAuthentication.SignOut();
                             Page.Response.Redirect(this._redirectUrl+"?SessionExpired=true);
                          }
                          Page.Response.Redirect(this._redirectUrl);

Open in new window

and on Login page's page_prerender you check for SessionExpried variable in querystring and display message or modal window.
0
 

Author Closing Comment

by:niceguy971
ID: 37895649
Thanks
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now