MVC中的管理员授权

时间:2016-10-07 08:46:33

标签: c# asp.net-mvc authorization

我创建了一个包含userID的cookie ..

C#

FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
    "UserID",
    DateTime.Now,
    DateTime.MaxValue,
    true,
    s.EmpID, // userID   
    FormsAuthentication.FormsCookiePath);
    // Encrypt the ticket.
    string encTicket = FormsAuthentication.Encrypt(ticket);
    // Create the cookie.
    Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket) {
        Expires = ticket.Expiration
    });

但问题是,当非管理员用户访问管理页面时,如何显示错误页面,指出权限被拒绝

 [Authorize(Roles = "Admin")]

1 个答案:

答案 0 :(得分:0)

我将尝试通过我在项目中的表现来解释这一点。

  1. 我在web.config和Authentication中添加了RoleProvider。 RoleProvider在类UserBs.cs的项目BLL中实现,而Authentication mode = Forms如果用户未获得View的授权,则将任何用户返回到/ Common / Login。
  2. enter image description here

    enter image description here

    1. 在类文件中实现RoleProvider的抽象类。在这里,您必须为getRolesForUser实现一个函数,该函数从数据库中获取特权/角色。
    2. enter image description here

      1. 最后一步是在每个控制器启动之前使用[Authorize(Roles =“ADMIN”)]来指定仅访问该页面的用户权限,或者仅指定[Authorize()]以便用户登录在访问任何页面之前。
      2. enter image description here]

相关问题