Solved

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

Posted on 2012-03-22
3
1,579 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
Comment Utility
0
 
LVL 9

Accepted Solution

by:
gery128 earned 250 total points
Comment Utility
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
Comment Utility
Thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

13 Experts available now in Live!

Get 1:1 Help Now