.NET Session Timeout

I searched, but I did not find the answer to this question. How can I get the value on the server, how much time is left before the session expires? My session settings:

// wait time, for example, 10 minutes

<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 the 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 the session time is less than half, and the user does some action. I get the initial value of 600, but it is not equal for the left session, because "slideExpiration" add some time (I do not know how much), but does not reset the session time on the left to the beginning of 10 minutes point.

How can I get the remaining session time before expiration?

+5
source share
3 answers

, :

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";
} 
+2

, reset ( ), , 10 , 10 . Clientside , , , 8- , , 2 ... , , .

setTimeout. javascript, .

0

Drasius, VB .

global.asa Session_Start -

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

-

    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 the mod - when I refresh the page, I get a reduced time before the timeout. However, the remaining time always shows 30 minutes, although I have the web.config setting as follows:

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

So I'm not sure if this approach gives a timeout that really synchronizes with the actual session timeout.

0
source

All Articles