简体   繁体   English

mvc超时Session_End

[英]mvc Timeout Session_End

I have an MVC application and I would like to redirect the user to the Home Screen just before the Session_End event fires? 我有一个MVC应用程序,我想在Session_End事件触发前将用户重定向到主屏幕吗?

If I allow it to go to session end then it just terminates the session and the page is no longer active. 如果我允许它进入会话结束,那么它将终止会话,并且该页面不再处于活动状态。

Any ideas on this? 有什么想法吗?

If the idea behind your question is just not let the session terminate automatically, you can do the following 如果您提出的问题只是不让会话自动终止,则可以执行以下操作

In your _Layout.cshtml, include a keep-alive pinging script. 在_Layout.cshtml中,包含一个保持活动的ping脚本。 I am showing in Razor syntax here - 我在这里显示Razor语法-

@{
   // Get the session timeout from configuration
   var sessionTimeout = (System.Web.Configuration.ConfigurationManager.GetSection("system.web/sessionState") as System.Web.Configuration.SessionStateSection).Timeout;
   // Ping interval can just a minute less that the session expiry
   var pingInterval = sessionTimeout.AddMinutes(-1.0);
}
<script type="text/javascript">
   window.setInterval(function () {
     $.ajax('@Url.Content("~")', { async: true, cache: false });
   }, @((int)pingInterval.TotalMilliseconds));
</script>

This script will ping your home page just one minute before the session expiry and thus kkeeping the session live. 该脚本将在会话到期前一分钟对您的主页执行ping操作,从而保持会话实时进行。

Use setInterval() and window.location.replace() . 使用setInterval()window.location.replace() The interval is in millisecond. 时间间隔以毫秒为单位。 So replace 3000 with with the interval you need. 因此,将3000替换为所需的时间间隔。

setInterval(function(){
    window.location.replace(http://www.google.com)
}, 3000);

setTimeout will also be a viable option in this scenario (@Dismissle) 在这种情况下, setTimeout也将是一个可行的选项(@Dismissle)

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

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