示例#1
0
        public async Task <IActionResult> ManageUserClaims(ManageUserClaimsViewModel model)
        {
            var user = await _userManager.FindByIdAsync(model.UserId);

            if (user == null)
            {
                ViewBag.ErrorMessage = $"User with id: {model.UserId} cannot be found.";
                return(View("NotFound"));
            }

            var claims = await _userManager.GetClaimsAsync(user);

            var result = await _userManager.RemoveClaimsAsync(user, claims);

            if (!result.Succeeded)
            {
                ModelState.AddModelError("", "Cannot remove existing user claims");
                return(View(model));
            }

            result = await _userManager.AddClaimsAsync(user, model.Claims
                                                       .Select(x => new Claim(x.ClaimType, x.IsSelected ? "true" : "false")));

            if (!result.Succeeded)
            {
                ModelState.AddModelError("", "Cannot add selected claims to the user");
                return(View(model));
            }

            return(RedirectToAction("EditUser", new { Id = model.UserId }));
        }
示例#2
0
        public async Task <IActionResult> ManageUserClaims(string userId)
        {
            var user = await _userManager.FindByIdAsync(userId);

            if (user == null)
            {
                ViewBag.ErrorMessage = $"User with id: {userId} cannot be found.";
                return(View("NotFound"));
            }

            var existingUserClaims = await _userManager.GetClaimsAsync(user);

            var model = new ManageUserClaimsViewModel
            {
                UserId = userId
            };

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

                if (existingUserClaims.Any(x => x.Type == claim.Type && x.Value == "true"))
                {
                    userClaim.IsSelected = true;
                }

                model.Claims.Add(userClaim);
            }

            return(View(model));
        }
示例#3
0
        public async Task <IActionResult> ManageUserClaims(ManageUserClaimsViewModel model, string userId)
        {
            var user = await userManager.FindByIdAsync(userId);

            if (user == null)
            {
                ViewBag.ErrorMEssage = $"User with Id = {userId} not found";
                return(View("NotFound"));
            }

            var allClaims = await userManager.GetClaimsAsync(user);

            var removeResult = await userManager.RemoveClaimsAsync(user, allClaims);

            if (!removeResult.Succeeded)
            {
                ModelState.AddModelError("", "Cannot remove user existing claims");
                return(View(model));
            }

            var claimsToAdd = model.UserClaims.Select(m => new Claim(m.ClaimType, m.IsSelected ? "true" : "false"));
            var addResult   = await userManager.AddClaimsAsync(user, claimsToAdd);

            if (!addResult.Succeeded)
            {
                ModelState.AddModelError("", "Cannot add roles to user");
                return(View(model));
            }

            return(RedirectToAction("EditUser", new { Id = userId }));
        }
示例#4
0
        public async Task <IActionResult> ManageUserClaims(string userId)
        {
            try
            {
                var user = await _userManager.FindByIdAsync(userId);

                if (user == null)
                {
                    Response.StatusCode      = 404;
                    ViewData["ErrorMessage"] = "404 Resource not found.";
                    return(View("Error"));
                }
                var existingUserClaims = await _userManager.GetClaimsAsync(user);

                var allClaims = await _repository.GetAllUserClaims();

                var allClaimsViewModel = _mapper.Map <List <ApplicationClaim>, List <ApplicationClaimViewModel> >(allClaims);

                ManageUserClaimsViewModel allInfo = new ManageUserClaimsViewModel()
                {
                    UserId             = userId,
                    UserEmail          = user.Email,
                    ExistingUserClaims = existingUserClaims,
                    AllClaims          = allClaimsViewModel
                };
                return(View("ManageUserClaims", allInfo));
            }
            catch (DbUpdateException dbex)
            {
                ViewData["ErrorMessage"] = "DB issue - " + dbex.Message;
                return(View("Error"));
            }
            catch (Exception ex)
            {
                ViewData["ErrorMessage"] = ex.Message;
                return(View("Error"));
            }
        }