How to get another User Claims or Add User to a Role in Asp.Net Core Application


Question: How do you get another User's #Claim other than the #Current #User in the Asp.Net Core Application?


Login to See the Rest of the Answer

Answer:
See the Code below:

If you want to add a User to the Role see the function below:

        [HttpPost]
        [Route("/Profile/MakeAdmin")]
        [Authorize(Roles = "Admin")]

        public async Task<IActionResult> AddUsersToRole()
        {
            string userID = Request.Form["userID"].ToString();//There is a better way to get Form Value from the HTTP Context, don't do this
            IdentityRole role = await _roleManager.FindByNameAsync("Admin");

            var user = await _userManager.FindByIdAsync(userID);
            if (!(await _userManager.IsInRoleAsync(user, role.Name)))
            {
                _ = await _userManager.AddToRoleAsync(user, role.Name);
                IList<Claim> checkIfUserHasThisClaim = await _userManager.GetClaimsAsync(user);

                if (!(checkIfUserHasThisClaim.Any(a => a.Type == role.Name)))
                {

                    Claim claim = new Claim(role.Name, user.Id, ClaimValueTypes.String);
                    IdentityResult result = await _userManager.AddClaimAsync(user, claim);//If you see an error that says "Cannot Insert the value of NULL
                    //Make sure the AspNetUserClaims Id Column is set as Primary Key and has Auto Increment turned on
                }
            }
            else
            {
                //Remove from the Role
                _ = await _userManager.RemoveFromRoleAsync(user, role.Name);
                IList<Claim> checkIfUserHasThisClaim = await _userManager.GetClaimsAsync(user);

                if ((checkIfUserHasThisClaim.Any(a => a.Type == role.Name)))
                {

                    Claim claim = new Claim(role.Name, user.Id, ClaimValueTypes.String);
                    IdentityResult result = await _userManager.RemoveClaimAsync(user, claim);
                }
            }

            return NoContent();
        }


If you want to utilize the User Claims on Runtime then look to retrieve the User and check the Claims.

IList<Profiles> AllProfiles = await _profile.getAllProfiles().ConfigureAwait(false);

            foreach (var profile in AllProfiles)
            {
                var user = await _userManager.FindByIdAsync(profile.UserId.ToString());
                IList<Claim> checkIfUserHasThisClaim = await _userManager.GetClaimsAsync(user);
                if ((checkIfUserHasThisClaim.Any(a => a.Type == "Admin")))
                {
                    profile.Claim = "Admin";//This property can have a [NotMapped] Data Annotation if you want to use it per request.

                }
                
            }
            return View(AllProfiles);


Leave us a comment if this helped you.









For peering opportunity Autonomouse System Number: AS401345 Custom Software Development at ErnesTech Email AddressContact: [email protected]