5

I searched but I dont`t find any specific answer for this question. How I can get value in server how much time left, before session expires? My session settings:

//timeout for example 10 min.

<authentication mode="Forms"> <forms name=".ASPXAUTH_External" loginUrl="Authentication/Unauthorized.aspx" protection="All" timeout="10" path="/" slidingExpiration="true" defaultUrl="~/Pages/home.aspx" cookieless="UseDeviceProfile" enableCrossAppRedirects="false"/>
</authentication>
<sessionState mode="InProc" timeout="10">
</sessionState>

I get initial value (it will get 10*60 = 600 seconds):

SessionStateSection sessionSection = (SessionStateSection)WebConfigurationManager.GetSection("system.web/sessionState");
countdown.Text = sessionSection.Timeout.TotalSeconds.ToString();

But when session time left less then half and user do some action. I get initial value 600, but it is not equal for left session time, because "slidingExpiration" add some time (I don`t know how much), but not resets session left time to starting 10 min point.

How can I get remaining session time until expiration?

3 Answers 3

2

I found that time of session expiration I can get like so:

DateTime dateNow = DateTime.Now;
if (HttpContext.Current.User.Identity is FormsIdentity)
{
    HttpCookie authCookie = this.Context.Request.Cookies[FormsAuthentication.FormsCookieName];
    FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
    double leftSeconds = (authTicket.Expiration - dateNow).TotalSeconds;
    // Control in MasterPage, where I setting value ant then taking for JavaSript to CountDown message
    countdown.Text = leftSeconds > 0 ? leftSeconds.ToString() : "0";
} 
Sign up to request clarification or add additional context in comments.

Comments

1

Following Drasius approach I coded a VB solution as follows.

In global.asa on Session_Start I have --

    Session("sessStart") = DateTime.Now
    Session("TO") = Session.Timeout

In page masterfile I have --

    Dim timemsg As String = ""
    Dim sstrt As DateTime = Session("sessStart")
    timemsg = "Strt=" & sstrt.ToShortTimeString() & " TO=" & Session("TO") & "<br />"
    Dim datenow = DateTime.Now
    Dim cusrn = HttpContext.Current.User.Identity.Name
    If cusrn <> "" Then
        Dim authcookie As System.Web.HttpCookie
        authcookie = HttpContext.Current.Request.Cookies(FormsAuthentication.FormsCookieName)
        Dim authTicket As System.Web.Security.FormsAuthenticationTicket
        authTicket = FormsAuthentication.Decrypt(authcookie.Value)
        Dim leftminutes = (authTicket.Expiration - datenow).TotalMinutes
        timemsg &= "Remain: "
        If leftminutes > 0 Then
            timemsg &= leftminutes.ToString("F1") & " mins"
        Else
            timemsg &= "0 mins"
        End If
    End If
    lblShowTime.Text = timemsg

This works after a fashion -- when I refresh page I get a reduced time to timeout. However, the initial time remaining always shows 30 minutes, even though I have web.config setting as follows:

<sessionState mode="InProc" timeout="20" />

So I'm not so certain that this approach is giving a timeout that is really synced with the actual session timeout.

Comments

0

If you are server side, it is reset (on any postback), so if you had 10 minutes, it is 10 minutes. Clientside you can set a timer after the last trip to server, so for example you can set an 8 minute timer and then warn that session expires in 2 minutes...Of course you would want to display the actual time left, not a static message.

Look at the setTimeout method. This link has info on some javascript timing methods you might use.

1 Comment

This. Not solves problem (sliding expiration, doent set session expiration to starting point (10 min.) it adds "some time".)... But when session time left less than half and user do some action. I get value 600 seconds, but it is not equal for left session time, because "slidingExpiration" add some time (I don`t know how much), but not resets session left time to starting 10 min point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.