locked
How to maintian session in a web application project?? RRS feed

  • Question

  • User-1568431074 posted

    Hi

    How to redirect to login page while session is expired????(which session value is asign at the time of login into the application)

    I am using .net 2008.In my web application,when login into the application through  login page.I am assigning some values to different session id like session("employeeid").I am using the session values(which is assign in the login page) in all pages of my application,If session not containg any values or session expired,it will redirect to the login page.

    Now the application containg more than 200 pages.I didn't use master page in my all pages also.So how I will handle the problem???

    So Plese suggest me ,I am waiting for your reply.

    Regards,

    Rakesh

     

    Wednesday, April 20, 2011 3:55 PM

Answers

  • User-668952958 posted

    Hi,

    It seems that you are using custom authentication for your application(ie, keeping values in the sessions). So in order to check if the session is expired, you need to do some checking in your pages, something like,

    if(Session["example"]==null)
    {
        // Redirect to Login page;
    }

    in your page load. but it seems you already have 200 pages and can have more. So I prefer you to create a new Class which will be a base class for all your aspx pages.

     

        public abstract class BasePage : Page
        {
            protected override void OnLoad(EventArgs e)
            {
                if (Session["Example"] == null)
                {
                    // Redirect to the login page;
                }
            }
        }

    This is just a code snippet, you can implement more in that.

    Give it try!!

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 21, 2011 2:21 AM

All replies

  • User1831358492 posted

    Have you go something like this in your Web.config

    <authentication mode="Forms">
      <forms loginUrl="Login.aspx" protection="All" timeout="60"></forms>
    </authentication>
    <authorization>
      <deny users="?"/>
    </authorization>

    This will mean that the user will be redirected back to the Login.aspx page.

    Wednesday, April 20, 2011 6:57 PM
  • User-668952958 posted

    Hi,

    It seems that you are using custom authentication for your application(ie, keeping values in the sessions). So in order to check if the session is expired, you need to do some checking in your pages, something like,

    if(Session["example"]==null)
    {
        // Redirect to Login page;
    }

    in your page load. but it seems you already have 200 pages and can have more. So I prefer you to create a new Class which will be a base class for all your aspx pages.

     

        public abstract class BasePage : Page
        {
            protected override void OnLoad(EventArgs e)
            {
                if (Session["Example"] == null)
                {
                    // Redirect to the login page;
                }
            }
        }

    This is just a code snippet, you can implement more in that.

    Give it try!!

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 21, 2011 2:21 AM
  • User-481631678 posted

    You can create a BasePae class which inherits System.Web.UI.Page. Override the OnInit method and write appropriate code in that which will redirect to default page.

    public class BasePage : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
           {
            if(Session["UserName"]==null)    
                {
                 Response.Redirect("~/Login.aspx");    
           }
        }
    }


    And inherit all your ASPX pages from BasePage class, instead of System.Web.UI.Page like this:-

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

     

    Thursday, April 21, 2011 8:21 AM