public async Task <IActionResult> ManageUserClaims(string userId) { var user = await userManager.FindByIdAsync(userId); if (user == null) { ViewBag.ErrorMessage = $"User with the respective ID:{userId} cannot be found."; return(View("NotFound")); } //To keep all the Claims in this object var userDataBaseClaims = await userManager.GetClaimsAsync(user); var model = new UserClaimsViewModels() { UserId = userId }; //ClaimStore contains the List of Claims(Claim type and Claim value) foreach (Claim claims in ClaimsStore.AllClaims) { //populate the UserClaim with the Type from the ClaimStore.AllClaims //is taken the "Type" from ClaimStore //So we can see the values displayed on the view (I mean the Claims Type) UserClaim userClaim = new UserClaim() { //ClaimType it is string ClaimType = claims.Type }; //Check in Database if the user has that specific Type(if it has set to true IsSelected) //"c" is a Claim that has the Type(string) //userDataBaseClaims contains all the Claims that were gathered "var userDataBaseClaims = await userManager.GetClaimsAsync(user);" //comparing if it has that Type. //populating IsSelected with True //if (userDataBaseClaims.Any(c => c.Type == userClaim.ClaimType)) // { // userClaim.IsSelected = true; //} if (userDataBaseClaims.Any(c => c.Type == userClaim.ClaimType && c.Value == "true")) //(added value in post) { userClaim.IsSelected = true; } //Add all the ClaimTypes and IsSelected to the ViewModel(UserClaimsViewModels()), so that can be displayed on the View model.Claims.Add(userClaim); } return(View(model)); }
public async Task <IActionResult> ManageUserClaims(UserClaimsViewModels model) { var user = await userManager.FindByIdAsync(model.UserId); if (user == null) { ViewBag.ErrorMessage = $"User with the respective ID:{model.UserId} cannot be found."; return(View("NotFound")); } //Get all Claims for the user var userClaims = await userManager.GetClaimsAsync(user); //Remove all Claims for the user //We avoid putting more conditions(if's), to test if the user is selected on the respective Claim, or not - //this is the reason why all claims are deleted var result = await userManager.RemoveClaimsAsync(user, userClaims); if (!result.Succeeded) { ModelState.AddModelError("", "Cannot remove user existing claims !"); return(View(model)); } //Here it checks for what is selected on the view, to add the claims to the user //I want to add just only what is selected, this is the reason that I use Where and Select. //("Where" returns Ienumarable of UserClaim and we need to return Ienumarable of Claim object(because of AddClaimAsync), this is the reason is put Select function, because return Ienumerable of Claim object) //result = await userManager.AddClaimsAsync(user, model.Claims.Where(c => c.IsSelected).Select(c => new Claim(c.ClaimType, c.ClaimType))); result = await userManager.AddClaimsAsync(user, model.Claims.Select(c => new Claim(c.ClaimType, c.IsSelected ? "true" : "false"))); if (!result.Succeeded) { ModelState.AddModelError("", "Cannot add selected claim to the user !"); return(View(model)); } return(RedirectToAction("EditUser", "Administration", new { Id = model.UserId })); //created an anonymous object because we need to return to the page of EditUser, so it needs the userId }