Solved

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

Posted on 2012-03-22
3
1,626 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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 …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

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