user2471435
user2471435

Reputation: 1674

How to add a user to a role?

There is existing code in my application that does this:

if (HttpContext.Current.User.IsInRole("Customer Account Admin"))
                    //
                    {
                    }
                    else
                    {
                        mi = radmenu1.Items.FindItemByText("Admin");
                        radmenu1.Items.Remove(mi);
                    }

What puts the user in that role We are NOT using the old Role Manager. We are using ASP.NET Identity 2.0 and this doesn't seem to do it:

var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();


    if (manager.AddToRole(manager.FindByName(UserName.Text).Id, "Customer Account Admin").Succeeded)
    {
       c.logActivity("Register.aspx.cs", "REG_USER_ROLE", "Setting user to Admin role succeeded");
                                    }
    }

Upvotes: 0

Views: 616

Answers (1)

Win
Win

Reputation: 62301

You can use IsInRole.

private ApplicationUserManager _userManager;

public ApplicationUserManager UserManager 
{
   get 
   {
      if (_userManager == null) 
      {
         _userManager = HttpContext.GetOwinContext()
             .GetUserManager<ApplicationUserManager>();
      }
      return _userManager;
   }
}

bool isAdmin = UserManager.IsInRole(User.Identity.GetUserId(), 
   "Customer Account Admin");

Upvotes: 1

Related Questions