簡體   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