?
Solved

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

Posted on 2012-03-22
3
Medium Priority
?
1,786 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 7

Assisted Solution

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

Accepted Solution

by:
gery128 earned 1000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

764 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