如何从会话中保存变量并在会话为空时获取

本文关键字:会话 获取 变量 保存 | 更新日期: 2023-09-27 18:35:09

I 写入会话用户登录。当会话被丢弃时,我需要在服务器端页面上获取用户登录。我尝试:

 private string currentUserLogin;
 protected void Page_Load(object sender, EventArgs e)
 {   
      if (!IsPostBack)
      {
          if (HttpContext.Current.Session["curUserRole"] == null)
          {
               // try to get a "currentUserLogin" varialbe, but it null!
          }
          else
          {
               currentUserLogin = HttpContext.Current.Session["curUserLogin"].ToString();                   
          }
      }
 }

如何从会话中保存变量并在会话为空时获取

将会话中的信息存储到 cookie 中(具有所需的过期时间),并在会话过期时从 cookie 中检索值。

在 Global.asax 中有一个 SessionEnd 事件。 我使用它来写入日志文件以写入用户 ID 和注销日期/时间。