javascript - ASP.net MVC session 超时持续时间

标签 javascript asp.net asp.net-mvc session asp.net-mvc-5

也许我想得太多了,但我需要做的是看看从开始 session 时间(1 分钟)减去当前时间的时间差是多少。

<script type="text/javascript">
var mySessionTimer;

@functions {        
    public int PopupShowDelay
    {
        get {
            DateTime currentSetTimeout = DateTime.Now.AddMinutes(HttpContext.Current.Session.Timeout);
            DateTime currentServerTime = DateTime.Now;
            TimeSpan duration = (currentServerTime.Subtract(currentSetTimeout));

            return 60000 * (int)duration.TotalMinutes;
        }
    }
}

function callJSSessionTimer() {
    var sessionTimeoutWarning = @PopupShowDelay;
    var sTimeout = parseInt(sessionTimeoutWarning);

    mySessionTimer = setTimeout('SessionEnd()', sTimeout);
}

function SessionEnd() {
    clearTimeout(mySessionTimer);
    window.location = "/Account/sessionover";
}

@if (userInfo != null)
{
    if (userInfo.chosenAMT == "True")
    {
        @:callJSSessionTimer();
    } else
    {
        @:clearTimeout(mySessionTimer);
    }
} else {
    @:clearTimeout(mySessionTimer);
}
</script>

因此,duration 的值为 -00:01:00,这在技术上是正确的,因为 currentSetTimeout1 分钟 并获取今天的日期/时间,从 currentSetTimeout 中减去该日期/时间后一分钟。

因此,所有这一切的重点是跟踪用户从一个页面跳转到另一个页面时的剩余 session 时间。目前,当用户转到另一个页面时,它会重置时间并且不准确。

我怎样才能按照我需要的方式做到这一点?

最佳答案

当用户在 session 期间转到另一个页面时,您可以使用 html5 session 存储来维护该值:

if (sessionStorage.popupShowDelay) {        
    sessionStorage.popupShowDelay = Number(sessionStorage.clickcount);
} else {
    DateTime currentSetTimeout = DateTime.Now.AddMinutes(HttpContext.Current.Session.Timeout);
    DateTime currentServerTime = DateTime.Now;
    TimeSpan duration = (currentServerTime.Subtract(currentSetTimeout));
    sessionStorage.popupShowDelay = 60000 * (int)duration.TotalMinutes;
}

您可以在此处查看更多信息: https://www.w3schools.com/html/html5_webstorage.asp

关于javascript - ASP.net MVC session 超时持续时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45719224/

相关文章:

javascript - 如何在 JavaScript 中同步播放音频文件?

javascript - 使用 Javascript 更改相对 div 的左侧位置在 firefox/chrome 中不起作用

asp.net - UniqueID 与 html 元素实际 id

c# - 如何检查 session 中是否没有数据表

javascript - MVC解析ajax到javascript excel

javascript - 具有多个选项的 Angular Directive(指令)

javascript - 根据用户ID隐藏div?

asp.net-mvc - MVC 4刷新局部 View

asp.net - 用于验证百分比值的范围验证器

c# - 没有 URL 查询字符串的 ASP.NET MVC 解析变量