ASP.NET WebForms - 如何授权访问页面

4
在最新的ASP.NET WebForms应用程序中,我们不再使用RoleManager等(据我所知),那么如何为特定角色授权访问网页?
在MVC中,我会使用Authorize属性,但在WebForms中不存在,所以我很困惑 - 有什么建议吗?

2个回答

2

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事件中检索特定Web表单的角色。
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("............");
         }
    }

如果您想在文件夹级别上进行授权,那么请在web.config文件中指定该文件夹的角色,而不是在webform上检查角色。
 <authorization>
  <allow  roles="Owner"/>
  <deny users="*"/>
</authorization>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接