Search code examples
asp.net-core-mvcrolesasp.net-identity-2

Role management in MVC 6


I am looking for role and permission management in MVC6. Basically my requirement is I will be having roles and those roles will have some set of permissions(Like Add,Edit,Delete,Modify etc.).

So, based on this I want to achieve following things

  1. Show/Hide content of view based on role and there permission for user.
  2. To restrict access to controller and its action methods.

Any help on this appreciated !


Solution

  • Partial answer:

    2: Just add attributes to your controller and/or methods, e.g.

    [Authorize(Roles = "Administrator, PowerUser")]
    public class ControlPanelController : Controller
    {
        public ActionResult SetTime()
        {
        }
    
        [Authorize(Roles = "Administrator")]
        public ActionResult ShutDown()
        {
        }
    }
    

    https://docs.asp.net/en/latest/security/authorization/roles.html