Global.asax中的ASP.NET会话

时间:2010-09-13 00:55:54

标签: asp.net

Global.asax中的 Application_AuthenticateRequest 方法中的会话始终为null。我已经尝试过:

this.Session,HttpContext.Current.Session 

始终为空。

  protected void Application_AuthenticateRequest()
    {
        string userRole = string.Empty;

        if (Request.IsAuthenticated)
        {
            if (this.Session["UserRole"] == null)
            {
               InsertSessionValue();
            }
            userRole =Session["UserRole"].ToString();
            HttpContext.Current.User = new GenericPrincipal(User.Identity, new string[] {userRole});
        }
    }

我也尝试使用Cache,但它不起作用,因为我需要每个用户的唯一信息。

如何在Global.asax中使用Session?HttpApplication Application属性是否对每个用户都是唯一的?

1 个答案:

答案 0 :(得分:13)

您现在无法在请求生命周期中使用Session,但它尚不可用/已填充,如果您想要使用它,您需要在生命周期的后期转移到事件,例如{ {3}}

相关问题