How to redirect to logon page when session State time out is completed in asp.net mvc

前端 未结 3 717
再見小時候
再見小時候 2020-12-13 03:06

I have an ASP.NET MVC4 application where I am implementing sessionTimeout like:


  

        
3条回答
  •  独厮守ぢ
    2020-12-13 03:24

    I discover very simple way to redirect Login Page When session end in MVC. I have already tested it and this works without problems.

    In short, I catch session end in _Layout 1 minute before and make redirection.

    I try to explain everything step by step.

    If we want to session end 30 minute after and redirect to loginPage see this steps:

    1. Change the web config like this (set 31 minute):

       
          
       
      
    2. Add this JavaScript in _Layout (when session end 1 minute before this code makes redirect, it makes count time after user last action, not first visit on site)

      
      
    3. Here is my LogOff Action, which makes only LogOff and redirect LoginIn Page

      public ActionResult LogOff()
      {
          Session["User"] = null; //it's my session variable
          Session.Clear();
          Session.Abandon();
          FormsAuthentication.SignOut(); //you write this when you use FormsAuthentication
          return RedirectToAction("Login", "Account");
      } 
      

    I hope this is a very useful code for you.

提交回复
热议问题