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

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

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


  

        
3条回答
  •  时光说笑
    2020-12-13 03:39

    One way is that In case of Session Expire, in every action you have to check its session and if it is null then redirect to Login page.

    But this is very hectic method To over come this you need to create your own ActionFilterAttribute which will do this, you just need to add this attribute in every action method.

    Here is the Class which overrides ActionFilterAttribute.

    public class SessionExpireFilterAttribute : ActionFilterAttribute
        {
            public override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                HttpContext ctx = HttpContext.Current;
    
                // check if session is supported
                CurrentCustomer objCurrentCustomer = new CurrentCustomer();
                objCurrentCustomer = ((CurrentCustomer)SessionStore.GetSessionValue(SessionStore.Customer));
                if (objCurrentCustomer == null)
                {
                    // check if a new session id was generated
                    filterContext.Result = new RedirectResult("~/Users/Login");
                    return;
                }
    
                base.OnActionExecuting(filterContext);
            }
        }
    

    Then in action just add this attribute like so:

    [SessionExpire]
    public ActionResult Index()
    {
         return Index();
    }
    

    This will do you work.

提交回复
热议问题