How to Check whether Session is Expired or not in asp.net

前端 未结 8 2123
情话喂你
情话喂你 2020-12-03 01:03

I\'ve specified the session timeout in web.config file. When the session is timeout I\'m not getting redirect to the login page but I am getting an error saying object refer

8条回答
  •  囚心锁ツ
    2020-12-03 01:41

    I use the @Adi-lester answer and add some methods.

    Method to verify if Session is Alive

    public static void SessionIsAlive(HttpSessionStateBase Session)
    {
        if (Session.Contents.Count == 0)
        {
            Response.Redirect("Timeout.html"); 
        }
        else
        {
            InitializeControls();
        }
    }
    

    Create session var in Page Load

    protected void Page_Load(object sender, EventArgs e)
    {
        Session["user_id"] = 1;
    }
    

    Create SaveData method (but you can use it in all methods)

    protected void SaveData()
    {
        // Verify if Session is Alive
        SessionIsAlive(Session);
    
        //Save Data Process
        // bla
        // bla
        // bla
    }
    

提交回复
热议问题