StealthRT
StealthRT

Reputation: 10542

ASP.net MVC session timeout duration

Perhaps I am over-thinking this but what I need to do is see what the time difference it is from the start session time (1 minute) subtracting the current time.

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

So for the value for duration is -00:01:00 which technically is correct since currentSetTimeout is 1 minute and it gets todays date/time which is a minute away since its subtracting it fromcurrentSetTimeout.

So the point of all of this is to keep track of the remaining session time when the user jumps from page to page. Currently when the user goes to another page, it resets the time and its not accurate.

How can I go about doing this the way I need it?

Upvotes: 0

Views: 1083

Answers (3)

StealthRT
StealthRT

Reputation: 10542

Got it!

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

Upvotes: 0

Pedro S Cord
Pedro S Cord

Reputation: 1359

You may use html5 session storage to maintain the value when a user goes to another page during the 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;
}

You can see more in formation here : https://www.w3schools.com/html/html5_webstorage.asp

Upvotes: 1

TJ Bandrowsky
TJ Bandrowsky

Reputation: 882

Conceptually your problem is the counter would reset as you go from page to page. So you have to keep start time of the session server side. If you are using ASP.NET, you'd use a session variable to do this. Other platforms have similar things. They all work by using cookies. Good luck!

Upvotes: 0

Related Questions