public async Task <IActionResult> RolesAssign(int id) { Profile pro = _profileDataService.GetSingle(p => p.ProfileId == id); // Get the user Id IdentityUser u = await _userManagerService.FindByNameAsync(pro.UserName); // IEnumerable<IdentityRole> rolex = _roleManagerService.Roles.OrderBy(x => x.Name); //// All Roles // Roles for a User ( just list) IList <string> roles = await _userManagerService.GetRolesAsync(u); //Roles Available List IEnumerable <IdentityRole> AllRoles = _roleManagerService.Roles.ToList(); //Roles Enum Complete IList <string> rolesAva = AllRoles.Select(x => x.Name).ToList(); // Roles List only Role Names IList <string> RolesAvalilable = rolesAva.Except(roles).ToList(); // Roles Available list after compare with user's roles RolesAssignViewModel vm = new RolesAssignViewModel { Roles = roles, User = pro.UserName, RolesAvalilable = RolesAvalilable, ProfileId = id, Message = HttpContext.Session.GetString(message) }; return(View(vm)); }
public async Task <IActionResult> RolesAssign(RolesAssignViewModel vm) { if (ModelState.IsValid) { // create the user IdentityUser u = await _userManagerService.FindByNameAsync(vm.User); if (vm.rol == null) { HttpContext.Session.SetString(message, "No Role left to perform the Action"); return(RedirectToAction("RolesAssign", "Profile", new { id = vm.ProfileId })); } else { if (vm.Action == "add") { await _userManagerService.AddToRoleAsync(u, vm.rol); HttpContext.Session.SetString(message, "Role added successfully"); } else if (vm.Action == "remo") { await _userManagerService.RemoveFromRoleAsync(u, vm.rol); HttpContext.Session.SetString(message, "Role removed successfully"); } else { } return(RedirectToAction("RolesAssign", "Profile", new { id = vm.ProfileId })); } } else { } return(RedirectToAction("RolesAssign", "Profile", new { id = vm.ProfileId })); }