public ActionResult EditUserRoles(UpdateUserRoleVM model) { //var userId = Request.Form[1].ToString(); //var rolIdler = Request.Form[2].ToString().Split(','); var userId = model.Id; var rolIdler = model.Roles; var roleManager = NewRoleManager(); var seciliRoller = new string[rolIdler.Count]; for (var i = 0; i < rolIdler.Count; i++) { var rid = rolIdler[i]; seciliRoller[i] = roleManager.FindById(rid).Name; } var userManager = NewUserManager(); var user = userManager.FindById(userId); foreach (var identityUserRole in user.Roles.ToList()) { userManager.RemoveFromRole(userId, roleManager.FindById(identityUserRole.RoleId).Name); } for (int i = 0; i < seciliRoller.Length; i++) { userManager.AddToRole(userId, seciliRoller[i]); } return(RedirectToAction("EditUser", new { id = userId })); }
public async Task <ActionResult> EditUserRoles(UpdateUserRoleVM model) { var userId = model.Id; var rolIdler = model.Roles; var roleManager = _membershipTools.RoleManager; var seciliRoller = new string[rolIdler.Count]; for (var i = 0; i < rolIdler.Count; i++) { var rid = rolIdler[i]; seciliRoller[i] = roleManager.FindByIdAsync(rid).Result.ToString(); } var userManager = _membershipTools.UserManager; var user = await userManager.FindByIdAsync(userId); var roles = _membershipTools.UserManager.GetRolesAsync(user).Result; foreach (var identityUserRole in roles) { await userManager.RemoveFromRoleAsync(user, identityUserRole); } for (int i = 0; i < seciliRoller.Length; i++) { await userManager.AddToRoleAsync(user, seciliRoller[i]); } return(RedirectToAction("EditUser", new { id = userId })); }