public ActionResult DeleteRoleForUser(string UserName, string RoleName) { var account = new AccountController(new ApplicationUserManager(new UserStore<ApplicationUser>(new ApplicationDbContext()))); ApplicationUser user = context.Users.Where(u => u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault(); if (account.UserManager.IsInRole(user.Id, RoleName)) { account.UserManager.RemoveFromRole(user.Id, RoleName); ViewBag.ResultMessage = "Role removed from this user successfully !"; } else { ViewBag.ResultMessage = "This user doesn't belong to selected role."; } // prepopulat roles for the view dropdown var list = context.Roles.OrderBy(r => r.Name).ToList().Select(rr => new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name }).ToList(); ViewBag.Roles = list; return View("ManageUserRoles"); }
public ActionResult GetRoles(string UserName) { if (!string.IsNullOrWhiteSpace(UserName)) { ApplicationUser user = context.Users.Where(u => u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault(); var account = new AccountController(new ApplicationUserManager(new UserStore<ApplicationUser>(new ApplicationDbContext()))); ViewBag.RolesForThisUser = account.UserManager.GetRoles(user.Id); // prepopulat roles for the view dropdown var list = context.Roles.OrderBy(r => r.Name).ToList().Select(rr => new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name }).ToList(); ViewBag.Roles = list; // populate userlist var userlist = context.Users.OrderBy(u => u.UserName).ToList().Select(uu => new SelectListItem { Value = uu.UserName.ToString(), Text = uu.UserName }).ToList(); ViewBag.UserList = userlist; } return View("ManageUserRoles"); }