简体   繁体   中英

Asp.net Mvc RedirectToAction not redirecting within account controller

In my Global.asax file I have an event Session_OnEnd which is fired when session timeout expires. Below is the code in Global.asax

protected void Session_OnEnd(object sender, EventArgs e)
{
        Controllers.AccountController obj = new 
             Controllers.AccountController();
        obj.RedirectoLogin();
}

Code in Account Controller

 internal void RedirectoLogin()
 {
        ViewBag.Message = "Session Expired";
        RedirectToAction("Login", "Account");
 }

 [AllowAnonymous]
 public ActionResult Login(string returnUrl)
 {
   ViewBag.ReturnUrl = returnUrl;
   return View();
 }

Method gets called . But doesn't get redirected to Login page.

The Session_OnEnd() method, gets called within the ASP.Net framework, on the server side. Since HTTP is stateless, the server cannot enforce a user to redirect to a new page (the connection is probably long-closed).

This method is intended to perform some clean-up (eg remove a database record for online users).

Further to @DavidG's comment, if you want to redirect the user, on the next request (when the browser sends the - now expired - session cookie) the user will be redirected to the login page.

If your web application relies heavily on client-side logic, a client-side timer that matches the session duration on the server, can trigger a redirect once the session has expired.

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