Esempio n. 1
0
        public async Task <IActionResult> ManageUserClaims(AdminUserClaimsViewModel viewModel)
        {
            var user = await userManager.FindByIdAsync(viewModel.ID.ToString());

            if (user == null)
            {
                return(NotFound());
            }

            var claims = await userManager.GetClaimsAsync(user);

            var removeClaims = await userManager.RemoveClaimsAsync(user, claims);

            if (!removeClaims.Succeeded)
            {
                ModelState.AddModelError("", "Could not remove existing claims for this user.");
                return(View(viewModel));
            }

            removeClaims = await userManager.AddClaimsAsync(user,
                                                            viewModel.Claims
                                                            .Where(c => c.IsSelected)
                                                            .Select(c => new Claim(c.ClaimType, c.ClaimType)));

            if (!removeClaims.Succeeded)
            {
                ModelState.AddModelError("", "Could not add selected claims for this user.");
                return(View(viewModel));
            }

            return(RedirectToAction("Edit", new { id = viewModel.ID }));
        }
Esempio n. 2
0
        public async Task <IActionResult> ManageUserClaims(int id)
        {
            var user = await userManager.FindByIdAsync(id.ToString());

            if (user == null)
            {
                return(NotFound());
            }

            var existingUserClaims = await userManager.GetClaimsAsync(user);

            AdminUserClaimsViewModel viewModel = new AdminUserClaimsViewModel
            {
                ID = id
            };

            foreach (Claim claim in UserClaimsStore.AllClaims)
            {
                UserClaim userClaim = new UserClaim
                {
                    ClaimType = claim.Type
                };

                if (existingUserClaims.Any(c => c.Type == claim.Type))
                {
                    userClaim.IsSelected = true;
                }

                viewModel.Claims.Add(userClaim);
            }

            return(View(viewModel));
        }