2010-09-13 92 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