2013-10-24 81 views

回答

1

在global.asax頁面添加此事件,用於檢查當前用戶是否logedin並將用戶詳細信息存儲在httpconext.current.user中。

protected void Application_AuthenticateRequest(Object sender, EventArgs e) 
{ 
    if (HttpContext.Current.User != null) 
    { 
     if (HttpContext.Current.User.Identity.IsAuthenticated) 
     { 
      if (HttpContext.Current.User.Identity is FormsIdentity) 
      { 
       FormsIdentity id = 
        (FormsIdentity)HttpContext.Current.User.Identity; 
       FormsAuthenticationTicket ticket = id.Ticket; 

       // Get the stored user-data, in this case, our roles 
       string userData = ticket.UserData; 
       string[] roles = userData.Split(','); 
       HttpContext.Current.User = new GenericPrincipal(id, roles); 
      } 
     } 
    } 
} 
0

AuthenticateRequest會觸發每個請求,允許您爲執行請求準備HttpContext。

相關問題