2010-09-13 88 views
8

会议在Application_AuthenticateRequest方法在Global.asax中总是null.Ive已经尝试:ASP.NET会话在Global.asax中

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}); 
     } 
    } 

我也尝试使用缓存,但它不工作,因为我需要每个用户的独特信息。

如何在Global.asax中使用Session?HttpApplication应用程序属性对每个用户都是唯一的吗?

+0

ID说它可能你没有正确地调用该方法。当没有请求正在处理时,发生NULL的原因。你如何调用该方法/分配该委托? – RPM1984 2010-09-13 01:00:27

+0

调用Application_AuthenticateRequest?当用户尝试进行身份验证时它会自动触发; – ozsenegal 2010-09-13 01:04:33

回答

13

您在请求生命周期的这一点上不能使用Session,但它尚未提供/尚未填充,如果要使用它,则需要在生命周期的稍后阶段移至事件,例如PostAcquireRequestState