简体   繁体   English

Asp.net Mvc RedirectToAction无法在帐户控制器内重定向

[英]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. 在我的Global.asax文件中,有一个Session_OnEnd事件,当会话超时过期时将触发该事件。 Below is the code in Global.asax 以下是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. 在服务器端在ASP.Net框架内调用Session_OnEnd()方法。 Since HTTP is stateless, the server cannot enforce a user to redirect to a new page (the connection is probably long-closed). 由于HTTP是无状态的,因此服务器无法强制用户重定向到新页面(该连接可能长时间关闭)。

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. 除了@DavidG的注释,如果要重定向用户,则在下一个请求(当浏览器发送-现在已过期-会话cookie)时,该用户将被重定向到登录页面。

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. 如果您的Web应用程序严重依赖客户端逻辑,则与会话在服务器上的持续时间匹配的客户端计时器可以在会话过期后触发重定向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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