是否可以使用 ASP.net 中的 sessionState 获取剩余会话超时
这是我的 webconfig 文件中的 sessionState 代码。
<sessionState
mode="SQLServer"
allowCustomSqlDatabase="true"
sqlConnectionString="my connection here"
timeout="100">
</sessionState>
Thanks
我知道这是一篇旧帖子,但没有正确的答案,我需要这个答案。
我已经使用了 Ashwin 的答案并使其发挥作用!
您需要在启动会话并开始会话的位置创建 session["SessionStart"] 变量。从那里开始就非常简单了,我返回秒作为值,这样我就可以使用 javascript 将秒更改为 hh:mm:ss,我还包含了一个 string.format 将秒转换为 hh:mm:ss。
// Get the session in minutes and seconds
HttpSessionState session = HttpContext.Current.Session;//Return current session
DateTime? sessionStart = session["SessionStart"] as DateTime?;//Get DateTime object SessionStart
//Check if session has not expired
if (sessionStart.HasValue)
{
TimeSpan timepassed = DateTime.Now - sessionStart.Value;//Get the time passed since the session was created
int TimeOut = (session.Timeout * 60) - Convert.ToInt32(remaining.TotalSeconds);
int h = (TimeOut / 3600);
int m = ((TimeOut / 60) % 60);
int s = TimeOut % 60;
SessionTimeoutValue.InnerText += TimeOut; // or use the string.format below.
//string.Format("{0}:{1}:{2}",
// h,
// m,
// s);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)