如何使用SessionState会获得剩余的会话超时? [英] How to get the remaining session timeout using SessionState?

查看:527
本文介绍了如何使用SessionState会获得剩余的会话超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能获得在ASP.net使用的sessionState剩余的会话超时

Is it possible to get the REMAINING session timeout using sessionState in ASP.net

下面是我在webconfig文件将sessionState code。

Here is the sessionState code in my webconfig file.

<sessionState 
  mode="SQLServer" 
  allowCustomSqlDatabase="true" 
  sqlConnectionString="my connection here" 
  timeout="100">
</sessionState>

感谢

推荐答案

是的,你可以使用下面的code,以获得剩余时间

Yes you can use the following code to get the remaining time

    HttpSessionState session = HttpContext.Current.Session;//Return current sesion
    DateTime? sessionStart = session[SessionKeys.SessionStart] as DateTime?;//Convert into DateTime object
    if(sessionStart.HasValue)//Check if session has not expired
    TimeSpan remaining = sessionStart.Value-DateTime.Now ;//Get the remaining time

这篇关于如何使用SessionState会获得剩余的会话超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆