2015-07-11 115 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>