public async Task <ActionResult> EditRoles(string userId)
        {
            var user = await userManager.FindByIdAsync(userId);

            var assignedRoles = await userManager.GetRolesAsync(user.Id);

            var allRoles = await roleManager.Roles.ToListAsync();

            var userRoles = allRoles.Select(r => new SelectListItem()
            {
                Value    = r.Name,
                Text     = r.Name,
                Selected = assignedRoles.Contains(r.Name),
            }).ToList();

            var viewModel = new UserRolesViewModel
            {
                Username  = user.UserName,
                UserId    = user.Id,
                UserRoles = userRoles,
            };

            return(View(viewModel));
        }
        public async Task <ActionResult> EditRoles(UserRolesViewModel viewModel)
        {
            var user = await userManager.FindByIdAsync(viewModel.UserId);

            var possibleRoles = await roleManager.Roles.ToListAsync();

            var userRoles = await userManager.GetRolesAsync(user.Id);

            var submittedRoles = viewModel.SelectedRoles;

            var shouldUpdateSecurityStamp = false;

            foreach (var submittedRole in submittedRoles)
            {
                var hasRole = await userManager.IsInRoleAsync(user.Id, submittedRole);

                if (!hasRole)
                {
                    shouldUpdateSecurityStamp = true;
                    await userManager.AddToRoleAsync(user.Id, submittedRole);
                }
            }

            foreach (var removedRole in possibleRoles.Select(r => r.Name).Except(submittedRoles))
            {
                shouldUpdateSecurityStamp = true;
                await userManager.RemoveFromRoleAsync(user.Id, removedRole);
            }

            if (shouldUpdateSecurityStamp)
            {
                await userManager.UpdateSecurityStampAsync(user.Id);
            }

            return(RedirectToAction("Index"));
        }
        public async Task<ActionResult> EditRoles(string userId)
        {
            var user = await userManager.FindByIdAsync(userId);
            var assignedRoles = await userManager.GetRolesAsync(user.Id);

            var allRoles = await roleManager.Roles.ToListAsync();

            var userRoles = allRoles.Select(r => new SelectListItem()
            {
                Value = r.Name,
                Text = r.Name,
                Selected = assignedRoles.Contains(r.Name),
            }).ToList();

            var viewModel = new UserRolesViewModel
                {
                    Username = user.UserName,
                    UserId = user.Id,
                    UserRoles = userRoles,
                };
            return View(viewModel);
        }
        public async Task<ActionResult> EditRoles(UserRolesViewModel viewModel)
        {
            var user = await userManager.FindByIdAsync(viewModel.UserId);
            var possibleRoles = await roleManager.Roles.ToListAsync();
            var userRoles = await userManager.GetRolesAsync(user.Id);

            var submittedRoles = viewModel.SelectedRoles;

            var shouldUpdateSecurityStamp = false;

            foreach (var submittedRole in submittedRoles)
            {
                var hasRole = await userManager.IsInRoleAsync(user.Id, submittedRole);
                if (!hasRole)
                {
                    shouldUpdateSecurityStamp = true;
                    await userManager.AddToRoleAsync(user.Id, submittedRole);
                }
            }

            foreach (var removedRole in possibleRoles.Select(r => r.Name).Except(submittedRoles))
            {
                shouldUpdateSecurityStamp = true;
                await userManager.RemoveFromRoleAsync(user.Id, removedRole);
            }

            if (shouldUpdateSecurityStamp)
            {
                await userManager.UpdateSecurityStampAsync(user.Id);
            }

            return RedirectToAction("Index");
        }