Solved

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

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 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