我搜索了但是我找不到这个问题的具体答案.
在会话到期之前,如何在服务器中获得剩余时间?
我的会话设置:
//超时,例如10分钟.
<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>
我得到初始值(它会得到10 * 60 = 600秒):
SessionStateSection sessionSection = (SessionStateSection)WebConfigurationManager.GetSection("system.web/sessionState"); countdown.Text = sessionSection.Timeout.TotalSeconds.ToString();
但是当会话时间少于一半时,用户会做一些动作.我得到初始值600,但它不等于左会话时间,因为“slidingExpiration”增加了一些时间(我不知道多少),但不会将会话剩余时间重置为开始10分钟.
如何在到期前获得剩余的会话时间?
解决方法
我发现会话到期的时间我可以这样:
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"; }