2009-07-18 103 views
3

我正在使用global.asaxApplication_OnPostAuthenticateRequest事件得到的Global.asax事件:Application_OnPostAuthenticateRequest

一)角色和身份驗證的用戶的權限,還我已經做了我的自定義主體類來獲取用戶詳細信息和角色和權限。

b)獲取一些信息,該信息對該用戶保持不變。

void Application_OnPostAuthenticateRequest(object sender, EventArgs e) 
{ 

    // Get a reference to the current User 
    IPrincipal objIPrincipal = HttpContext.Current.User; 

    // If we are dealing with an authenticated forms authentication request 
    if ((objIPrincipal.Identity.IsAuthenticated) && (objIPrincipal.Identity.AuthenticationType == "Forms")) 
    { 
     CustomPrincipal objCustomPrincipal = new CustomPrincipal(); 
     objCustomPrincipal = objCustomPrincipal.GetCustomPrincipalObject(objIPrincipal.Identity.Name); 
     HttpContext.Current.User = objCustomPrincipal; 
     CustomIdentity ci = (CustomIdentity)objCustomPrincipal.Identity; 
     HttpContext.Current.Cache["CountryID"] = FatchMasterInfo.GetCountryID(ci.CultureId); 
     HttpContext.Current.Cache["WeatherLocationID"] = FatchMasterInfo.GetWeatherLocationId(ci.UserId); 
     Thread.CurrentPrincipal = objCustomPrincipal; 
    } 
} 

我的問題是:爲每個請求每次

  1. 此事件。因此,對於每個請求代碼執行?
  2. 我的方法是對還是不對?
  3. 是正確添加HttpContext.Current.Cache在這種情況下,還是應該將它移動到在session_start

回答

4
  1. 是此事件的每個請求
  2. 是的,你可以使用這個事件來獲取信息對於通過身份驗證的用戶
  3. 不,不使用HttpCurrent.Current.Cache來存儲用戶特定的信息,因爲緩存對於所有用戶都很常見,並且您會遇到衝突。改爲使用HttpContext.Current.Session,因爲這將特定於用戶。
+0

Thnaks, 再次提高几倍? 如果此事件針對每個請求觸發,那麼對於每個請求,我將執行與角色和授權的數據庫連接。這是否好,我們不能在會話啓動事件中做同樣的事情。 – 2009-07-18 11:22:25

相關問題