是否可以使用 ASP.net 中的 sessionState 获取剩余 session 超时
这是我的 webconfig 文件中的 sessionState 代码。
<sessionState
mode="SQLServer"
allowCustomSqlDatabase="true"
sqlConnectionString="my connection here"
timeout="100">
</sessionState>
谢谢
最佳答案
我知道这是一篇旧帖子,但没有正确的答案,我需要这个答案。
我在某种程度上使用了 Ashwin 的答案并使其发挥作用!
您需要在启动 session 的位置创建 session["SessionStart"] 变量并开始 session 。从那里开始就非常简单了,我返回秒作为值,这样我就可以使用 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);
}
关于javascript - 如何使用SessionState获取剩余 session 超时时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11425183/