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



也许我想多了,但我需要做的是看看从开始会话时间(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>

因此,持续时间的值为 -00:01:00,这在技术上是正确的,因为当前设置超时是1分钟,并且它得到今天的日期/时间,距离它从当前设置超时中减去它还有一分钟。

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

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

当用户在会话期间转到另一个页面时,您可以使用 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,则应使用会话变量来执行此操作。其他平台也有类似的东西。它们都通过使用饼干来工作。祝你好运!

知道了!

@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;
}
}
}

最新更新