Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Session Timeout .NET

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?

like image 205
Drasius Avatar asked Oct 21 '22 16:10

Drasius


1 Answers

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";
} 
like image 127
Drasius Avatar answered Oct 27 '22 23:10

Drasius