繁体   English   中英

ASP.net MVC会话超时持续时间

[英]ASP.net MVC session timeout duration

也许我对此考虑过多,但是我需要做的是看看从开始会话时间(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中减去的时间,所以是一分钟。

因此,所有这些的重点是跟踪用户在页面之间跳转时的剩余会话时间 当前,当用户转到另一个页面时,它将重置时间及其不准确的时间。

我该如何按照自己的需要去做?

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

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

从概念上讲,您的问题是计数器会在您翻页时重置。 因此,您必须保留会话服务器端的开始时间。 如果您使用的是ASP.NET,则可以使用会话变量来执行此操作。 其他平台也有类似的事情。 它们都通过使用cookie来工作。 祝好运!

得到它了!

@functions {        
        public int PopupShowDelay
        {
            get {
                if (Session["currentSetTimeout"] != null)
                {
                    DateTime dt1 = DateTime.Parse(Session["currentSetTimeout"].ToString());
                    DateTime dt2 = DateTime.Parse(DateTime.Now.ToString());
                    TimeSpan span = dt1 - dt2;

                    return (int)span.TotalMilliseconds;
                }

                return 0;
            }
        }
    }

暂无
暂无

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

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