2015-07-11 109 views
1

在最新的ASP.NET WebForms應用程序中,我們不再使用RoleManager等(據我所知),那麼我們如何授權訪問特定角色的網頁?ASP.NET WebForms - 如何授權訪問頁面

在MVC中,我會使用Authorize屬性,但WebForms中不存在,所以我不知所措 - 任何想法?

+0

的可能的複製[有一個authorizeattribute只相當於標準的web形式(不MVC)的.NET(http://stackoverflow.com/問題/ 4217576/IS-有-AN-authorizeattribute等效到只是標準的web表單 - 不MVC-F) –

回答

1

嘗試登錄該代碼 傳遞作用的FormsAuthenticationTicket

FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, UserName.Text, DateTime.Now, DateTime.Now.AddMinutes(2880), false, role, FormsAuthentication.FormsCookiePath); 
      string hash = FormsAuthentication.Encrypt(ticket); 
      HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash); 

      if (ticket.IsPersistent) 
      { 
       cookie.Expires = ticket.Expiration; 
      } 
      Response.Cookies.Add(cookie); 
      Response.Redirect(FormsAuthentication.GetRedirectUrl(UserName.Text, false)); 

上Page_Load事件尤其是網絡表格檢索角色

protected void Page_Load(object sender, EventArgs e) 
    { 

      FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity; 
      FormsAuthenticationTicket ticket = id.Ticket; 
      string userData = ticket.UserData; 
      string[] temp = userData.Split(','); 
      role=temp[0]; 
     if (role!="Owner") 
     { 
      Response.Write("............"); 
     } 
    } 

如果您想在文件夾級別授權,那麼不要檢查rol E在網路表單中指定該文件夾的web.config文件的作用

<authorization> 
    <allow roles="Owner"/> 
    <deny users="*"/> 
</authorization>