public async Task <IActionResult> Assign(RoleUservm roleUser) { var user = _db.ApplicationUsers.FirstOrDefault(c => c.Id == roleUser.UserId); var isassigned = await _userManager.IsInRoleAsync(user, roleUser.RoleId); if (isassigned) { ViewBag.msg = "This user's role assigned already."; ViewData["Userid"] = new SelectList(_db.ApplicationUsers.Where(c => c.LockoutEnd < DateTime.Now || c.LockoutEnd == null).ToList(), "Id", "UserName"); ViewData["roleid"] = new SelectList(_roleManager.Roles.ToList(), "Name", "Name"); return(View()); } var role = await _userManager.AddToRoleAsync(user, roleUser.RoleId); if (role.Succeeded) { TempData["save"] = "User Role assigned."; return(RedirectToAction(nameof(Index))); } return(View()); }
public async Task <IActionResult> Assign(RoleUservm vm) { var user = _db.user.FirstOrDefault(c => c.Id == vm.UserId); var checking = await userManager.IsInRoleAsync(user, vm.RoleId); if (checking) { ViewBag.msg = "Este Usuario ya tiene asignado este rol"; ViewData["UserId"] = new SelectList(_db.user.Where(f => f.LockoutEnd < DateTime.Now || f.LockoutEnd == null).ToList(), "Id", "UserName"); ViewData["RoleId"] = new SelectList(roleManager.Roles.ToList(), "Name", "Name"); return(View()); } var role = await userManager.AddToRoleAsync(user, vm.RoleId); if (role.Succeeded) { TempData["Save"] = "El rol ha sido asignado correctamente"; return(RedirectToAction(nameof(Index))); } return(View()); }