【问题标题】:how to get the session id and name in global.asax page如何在 global.asax 页面中获取会话 ID 和名称
【发布时间】:2014-08-07 14:12:37
【问题描述】:

我在登录页面时设置了会话 ID 和名称。现在我尝试在 Application_Error() 方法中获取 global.asax page 中的会话 ID 和名称。

我编写了以下代码来获取以上 2 个值。

但是这两个值总是 null 。如何在侧 Application_Error 方法中获取这两个值。

void Application_Error(object sender, EventArgs e) 
{
      string  userId = Session["UserId"].ToString();
      string  userName = Session["UserName"].ToString();
}

【问题讨论】:

    标签: c# asp.net session null global-asax


    【解决方案1】:

    要获取会话 ID,请使用:

    HttpContext.Current.Session.SessionID
    

    获取用户名(注意,当用户未登录时,这个可以为空):

    HttpContext.Current.User.Identity.Name
    

    【讨论】:

      【解决方案2】:

      您不会在 Application_Error 中获得任何会话值。

      Read more

      【讨论】:

        【解决方案3】:

        试试这个

        string userId;
        string userName;
        if (HttpContext.Current != null && HttpContext.Current.Session != null) {
            userId= HttpContext.Current.Session["UserId"];
            userName= HttpContext.Current.Session["UserName"];
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-10-13
          • 1970-01-01
          • 2022-08-02
          • 1970-01-01
          • 1970-01-01
          • 2012-01-31
          • 2011-05-17
          相关资源
          最近更新 更多