public ActionResult DeleteRoleForUser(string UserName, string RoleName) { using (var context = new ApplicationDbContext()) { var account = new AccountController(); 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 RoleAddToUser(string UserName, string RoleName) { using (var context = new ApplicationDbContext()) { ApplicationUser user = context.Users.Where(u => u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault(); if (user != null) { if (!Roles.IsUserInRole(UserName, RoleName)) { var account = new AccountController(); account.UserManager.AddToRole(user.Id, RoleName); ViewBag.ResultMessage = "Role created successfully !"; } else { ViewBag.ResultMessage = "This user already has the role specified !"; } } else { ViewBag.ResultMessage = "Username no exist!"; } //ApplicationUser user = context.Users.Where(u => u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault(); //var account = new AccountController(); //account.UserManager.AddToRole(user.Id, RoleName); //ViewBag.ResultMessage = "Role created successfully !"; // 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) { using (var context = new ApplicationDbContext()) { if (!string.IsNullOrWhiteSpace(UserName)) { ApplicationUser user = context.Users.Where(u => u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault(); var account = new AccountController(); 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; } return View("ManageUserRoles"); } }