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

前端 未结 8 2108
情话喂你
情话喂你 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:46

    this way many people detect session has expired or not. the below code may help u.

    protected void Page_Init(object sender, EventArgs e)
        {
            if (Context.Session != null)
            {
                if (Session.IsNewSession)
                {
                    HttpCookie newSessionIdCookie = Request.Cookies["ASP.NET_SessionId"];
                    if (newSessionIdCookie != null)
                    {
                        string newSessionIdCookieValue = newSessionIdCookie.Value;
                        if (newSessionIdCookieValue != string.Empty)
                        {
                            // This means Session was timed Out and New Session was started
                            Response.Redirect("Login.aspx");
                        }
                    }
                }
            }
        }
    

提交回复
热议问题