locked
WebForms - Catch session TimeOut RRS feed

  • Question

  • User-195907812 posted

    Hi,

    How would I catch a session timeout in the site.master code-behind (C#) if I'm not using forms authentication?

    I was using the following code, but this incorrectly executes the first time the page is loaded.

            if (Session["USER_NAME"] == null)
            {
                Response.Redirect("/timeout.aspx");
            }

    Thanks

    Thursday, November 12, 2015 8:35 AM

Answers

  • User475983607 posted

    You'll need to allow the Session to be null if the user requested the login page.  Something like the following.  Double check my syntax but you should get the idea.

    if (Session["USER_NAME"] == null & !HttpContext.Current.Request.Url.AbsoluteUri.ToString().Contains("login.aspx"))
    {
       Response.Redirect("/timeout.aspx");
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 12, 2015 9:44 AM
  • User-986267747 posted

    Hi RageRiot,

    I was using the following code, but this incorrectly executes the first time the page is loaded.

    As mgebhard say, you need to allow the Session to be null if the user requested the login page so that it could solve your problem. Besides, in my experience, you could create a basepage class instead of the page class to provide to other pages to inherit.

     public partial class BasePage : System.Web.UI.Page
        {
            protected override void OnLoad(EventArgs e)
            {
                BaseLoad();
                base.OnLoad(e);
            }
            protected void BaseLoad()
            {
                if (Session["USERNAME"] == null)
                {
                    Response.Redirect("/About.aspx");
                }
    
            }
        }

    When the some page need to verify if the user has logged in, you could make the page to inherit the basepage class.

    namespace GridViewAppear
    {
        public partial class Test : BasePage
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
        }
    }

    For the some page don't need to verify if the user has logged in, you could  inherit the page class directly.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace GridViewAppear
    {
        public partial class Login : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
        }
    }

    This is a common design pattern for a long time ago.

    I hope it's helpful to you.

    Best Regards,

    Klein zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 13, 2015 12:16 AM

All replies

  • User475983607 posted

    You'll need to allow the Session to be null if the user requested the login page.  Something like the following.  Double check my syntax but you should get the idea.

    if (Session["USER_NAME"] == null & !HttpContext.Current.Request.Url.AbsoluteUri.ToString().Contains("login.aspx"))
    {
       Response.Redirect("/timeout.aspx");
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 12, 2015 9:44 AM
  • User-986267747 posted

    Hi RageRiot,

    I was using the following code, but this incorrectly executes the first time the page is loaded.

    As mgebhard say, you need to allow the Session to be null if the user requested the login page so that it could solve your problem. Besides, in my experience, you could create a basepage class instead of the page class to provide to other pages to inherit.

     public partial class BasePage : System.Web.UI.Page
        {
            protected override void OnLoad(EventArgs e)
            {
                BaseLoad();
                base.OnLoad(e);
            }
            protected void BaseLoad()
            {
                if (Session["USERNAME"] == null)
                {
                    Response.Redirect("/About.aspx");
                }
    
            }
        }

    When the some page need to verify if the user has logged in, you could make the page to inherit the basepage class.

    namespace GridViewAppear
    {
        public partial class Test : BasePage
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
        }
    }

    For the some page don't need to verify if the user has logged in, you could  inherit the page class directly.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace GridViewAppear
    {
        public partial class Login : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
        }
    }

    This is a common design pattern for a long time ago.

    I hope it's helpful to you.

    Best Regards,

    Klein zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 13, 2015 12:16 AM