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

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

    Edit

    You can use the IsNewSession property to check if the session was created on the request of the page

    protected void Page_Load() 
    { 
       if (Context.Session != null) 
       { 
          if (Session.IsNewSession) 
          { 
             string cookieHeader = Request.Headers["Cookie"]; 
             if ((null != cookieHeader) && (cookieHeader.IndexOf("ASP.NET_SessionId") >= 0)) 
             { 
                Response.Redirect("sessionTimeout.htm"); 
             } 
          } 
       } 
    }
    

    pre

    Store Userid in session variable when user logs into website and check on your master page or created base page form which other page gets inherits. Then in page load check that Userid is present and not if not then redirect to login page.

    if(Session["Userid"]==null)
    {
      //session expire redirect to login page 
    }
    

提交回复
热议问题