会话超时.NET [英] Session Timeout .NET
问题描述
我搜查,但我dont`t找到这个问题的任何具体的答案。 我怎样才能在服务器上获取的价值有多少时间留给,会话过期之前? 我的会话设置:
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:
//超时,例如10分钟。
//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>
我得到的初始值(它会得到10 * 60 = 600秒):
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();
但是,当会议时间留给不到一半的用户做一些动作。我得到的初始值600,但它是不相等的,离开了会议的时间,因为slidingExpiration添加一些时间(我不`吨知道有多少),但不能复位会话离开时间开始10分钟点。
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.
我怎样才能获得剩余会话时间,直到过期?
推荐答案
我发现会话过期的那段时间,我可以得到像这样:
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";
}
这篇关于会话超时.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!