예제 #1
0
        public async Task <IActionResult> ManageUserClaims(string id)
        {
            var user = await _userManager.FindByIdAsync(id);

            if (user == null)
            {
                ViewBag.ErrorMessage = $"User with ID = {id} do not exist.";
                return(View("Error", "Admin"));
            }

            var existingUserClaims = await _userManager.GetClaimsAsync(user);

            ManageUserClaimsVM model = new ManageUserClaimsVM
            {
                user_id = id
            };

            foreach (Claim i in ClaimsStore.AllClaims)
            {
                UserClaim claim = new UserClaim
                {
                    claim_type = i.Type
                };

                if (existingUserClaims.Any(c => c.Type == i.Type))
                {
                    claim.isInClaim = true;
                }
                model.claims.Add(claim);
            }


            return(View(model));
        }
예제 #2
0
        public async Task <IActionResult> ManageUserClaims(ManageUserClaimsVM model, string user_id, string claim_type)
        {
            var user = await _userManager.FindByIdAsync(user_id);

            if (user == null)
            {
                ViewBag.ErrorMessage = $"User with ID = {user_id} do not exist.";
                return(View("Error", "Admin"));
            }

            var claims = await _userManager.GetClaimsAsync(user);

            IdentityResult result = null;

            bool hasClaim = false;

            foreach (Claim i in claims)
            {
                if (i.Type == claim_type)
                {
                    hasClaim = true;
                }
            }

            if (hasClaim)
            {
                result = await _userManager.RemoveClaimAsync(user, new Claim(claim_type, claim_type));
            }
            else
            {
                result = await _userManager.AddClaimAsync(user, new Claim(claim_type, claim_type));
            }

            if (!result.Succeeded)
            {
                ModelState.AddModelError(string.Empty, "There is something wrong with managing claims!");
                return(View());
            }
            return(RedirectToAction("ManageUserClaims", new { id = user.Id }));
        }