0

If I have a controller which has [authorize(role="admin")], how do I create the admin role and add a user to the role? Many people suggested I create a new class and many other changes but it's quite difficult to implement this and I'm wondering if there is a simpler way.

Here's what I have found.

Where should I put this?

        Roles.CreateRole("admin");

and where should i implement this

        Roles.AddUserToRole(User.Identity.Name,"admin");
doobop
  • 4,465
  • 2
  • 28
  • 39

1 Answers1

0

You may use it in an ActionResult and it looks like this depending on implementation :

    public ActionResult Create(string RoleName)
    {
        Roles.CreateRole(RoleName);
        return RedirectToAction("Index");
    }

And again for adding role:

 public ActionResult AddToAdmin(User user)
    {           
        Roles.AddUserToRole(user.UserName, "admin");                    
        return RedirectToAction("Index");             
    }
G.Anıl Yalçın
  • 188
  • 1
  • 14
  • there's an exception happened when i'm creating a new role that says i have to enable the rolemanger can you help me please i'm really confused with identity – Ahmad Tarabeshi Aug 17 '16 at 18:32