简体   繁体   中英

How to auto redirect to login page when session expires on a site with master page

I am working on an asp.net webforms project with master page. I would like the website auto redirect to the login.aspx page when the session ends. I tried the suggestion given at the following link. https://code.msdn.microsoft.com/Auto-redirect-to-login-e1782b2f Essentially I have copied the javascript from the link to the master page's aspx head section and used the C# code in the master page's page load event. But it doesn't redirect to the login page after the set 3 min session timeout. I have the following entry in my web.config file.

<sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" 
sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes" 
cookieless="false" timeout="3" />

I even tried through the global.ascx page as suggested by this link, which didn't work either. http://www.c-sharpcorner.com/UploadFile/0c1bb2/redirect-page-after-session-time-out-in-Asp-Net424/ Thanks

You can check session in master page like:

protected void Page_Load(object sender, EventArgs e)
  {
    if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
    {
        if (!IsPostBack)
        {
           //User session active
        }
    }
    else
    {
        //User session not active
        Response.Redirect("Login.aspx", false);
    }
}

And Update Web.config like:

<authentication mode="Forms">
  <forms loginUrl="~/Login.aspx" protection="All" timeout="3000" path="/" />
</authentication>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM