0

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"); } } 

1 Answer 1

1

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"); 
Sign up to request clarification or add additional context in comments.

3 Comments

Doesn't that just check that he is that role? I want to add him to the role.
I don't need that. I don't need to find out if he is in the role. I need to add him to that role.
You original said How to get user in a role? Anyhow, AddToRole should work. Have you try var roleResult = await UserManager.AddUserToRolesAsync(user.Id, RoleNames);? The AddUserToRolesAsync helper method comes with Microsoft ASP.NET Identity Samples

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.