コード例 #1
0
ファイル: AdminController.cs プロジェクト: AmosWhite/VTU
        public async Task <IActionResult> ManageUserClaims(UserClaimsVM model)
        {
            var user = await userManager.FindByIdAsync(model.UserId);

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

            // Get all the user existing claims and delete them
            var claims = await userManager.GetClaimsAsync(user);

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

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

            // Add all the claims that are selected on the UI
            result = await userManager.AddClaimsAsync(user,
                                                      model.Cliams.Where(c => c.IsSelected).Select(c => new Claim(c.ClaimType, c.ClaimType)));

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

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

            if (user != null)
            {
                var claims = await userManager.GetClaimsAsync(user);

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

                if (!result.Succeeded)
                {
                    ModelState.AddModelError("", "Cannot remove user existing 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 user existing claims");
                    return(View(model));
                }

                return(RedirectToAction("EditUser", new { Id = model.UserId }));
            }

            ViewBag.ErrorMessage = $"User with Id = {userId} cannot be found";
            return(View("NotFound"));
        }
コード例 #3
0
        public async Task <IActionResult> ManageUserClaims(string userId)
        {
            var user = await userManager.FindByIdAsync(userId);

            if (user != null)
            {
                var existingUserClaims = await userManager.GetClaimsAsync(user);

                var model = new UserClaimsVM
                {
                    UserId = userId
                };

                foreach (Claim claim in ClaimsStore.AllClaims)
                {
                    UserClaim 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));
            }

            ViewBag.ErrorMessage = $"User with Id = {userId} cannot be found";
            return(View("NotFound"));
        }
コード例 #4
0
        public async Task <IActionResult> Index()
        {
            var userClaimsVM = new UserClaimsVM();
            var userClaimsWithClientCredentials = await GetUserClaimsFromApiWithClientCredentials();

            userClaimsVM.UserClaimsWithClientCredentials = userClaimsWithClientCredentials.IsSuccessStatusCode ? await userClaimsWithClientCredentials.Content.ReadAsStringAsync() : userClaimsWithClientCredentials.StatusCode.ToString();

            return(View(userClaimsVM));
        }
コード例 #5
0
        public async Task <IActionResult> AddUserClaims(string id, UserClaimsVM vm)
        {
            var user = await _userManager.FindByIdAsync(id);

            if (user == null)
            {
                return(NotFound("User not found"));
            }

            if (!await IsLoggedInUserGlobalAdmin()) //Either global or super
            {
                if (await _userManager.IsInRoleAsync(user, "Global") || await _userManager.IsInRoleAsync(user, "Super"))
                {
                    return(RedirectToAction("AccessDenied", "Authorization"));
                }
            }

            var claims = new List <Claim>();

            if (!string.IsNullOrWhiteSpace(vm.NewClaimKey1))
            {
                claims.Add(new Claim(vm.NewClaimKey1, vm.NewClaimValue1));
            }
            if (!string.IsNullOrWhiteSpace(vm.NewClaimKey2))
            {
                claims.Add(new Claim(vm.NewClaimKey2, vm.NewClaimValue2));
            }
            if (!string.IsNullOrWhiteSpace(vm.NewClaimKey3))
            {
                claims.Add(new Claim(vm.NewClaimKey3, vm.NewClaimValue3));
            }
            if (!string.IsNullOrWhiteSpace(vm.NewClaimKey4))
            {
                claims.Add(new Claim(vm.NewClaimKey4, vm.NewClaimValue4));
            }
            if (!string.IsNullOrWhiteSpace(vm.NewClaimKey5))
            {
                claims.Add(new Claim(vm.NewClaimKey5, vm.NewClaimValue5));
            }

            if (claims.Any())
            {
                await _userManager.AddClaimsAsync(user, claims);
            }
            return(RedirectToAction("EditUserClaims", "Users", new { id = user.Id }));
        }
コード例 #6
0
ファイル: AdminController.cs プロジェクト: AmosWhite/VTU
        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"));
            }

            // UserManager service GetClaimsAsync method gets all the current claims of the user
            var existingUserClaims = await userManager.GetClaimsAsync(user);

            var model = new UserClaimsVM
            {
                UserId = userId
            };

            // Loop through each claim we have in our application
            foreach (Claim claim in ClaimsStore.AllClaims)
            {
                UserClaim userClaim = new UserClaim
                {
                    ClaimType = claim.Type
                };

                // If the user has the claim, set IsSelected property to true, so the checkbox
                // next to the claim is checked on the UI
                if (existingUserClaims.Any(c => c.Type == claim.Type))
                {
                    userClaim.IsSelected = true;
                }

                model.Cliams.Add(userClaim);
            }

            return(View(model));
        }
コード例 #7
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 UserClaimsVM
            {
                UserId = userId
            };

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

                // If the user has the claim, set IsSelected property to true, so the checkbox
                // next to the claim is checked on the UI
                if (existingUserClaims.Any(c => c.Type == claim.Type && c.Value == "true"))
                {
                    userClaim.IsSelected = true;
                }

                model.Cliams.Add(userClaim);
            }

            return(View(model));
        }