Search code examples
c#asp.net-core-2.1asp.net-core-identity

Selecting Identity User Claims giving error


    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> EditUser(EditUserViewModel viewModel)
    {
        if (!ModelState.IsValid) return View();

        var user = await _userManager.FindByIdAsync(viewModel.Id);

        if (user == null) return RedirectToAction("Index", "Admin");

        var userClaims = await _userManager.GetClaimsAsync(user);
        
        if (viewModel.Admin)
        {
            if (!userClaims.Select(c => c.Value == "Admin").Any())
                await _userManager.AddClaimAsync(user, new Claim("Admin", "Admin"));
        }
        else
        {
            if (userClaims.Select(c => c.Value == "Admin").Any())
                await _userManager.RemoveClaimAsync(user, new Claim("Admin", "Admin"));
        }

        user.FirstName = viewModel.FirstName;
        user.LastName = viewModel.LastName;

        var result = await _userManager.UpdateAsync(user);

        if (result.Succeeded) return RedirectToAction("Index", "Admin");

        ModelState.AddModelError("", "Something went wrong");

        return View();
    }

When checking Admin claim exists giving error:

c.Value=error CS0103: The name 'c' does not exist in the current context

The thread 0x990 has exited with code 0 (0x0).

Error Error image


Solution

  • As discussed in the comments, there is nothing wrong. Visual Studio is just showing you the potential value of c but it is not in the scope yet.

    As you have seen now, as soon as you put in the breakpoint in the Linq Select, c does have a value.

    Finally, the thread message is a regular message indicating that some thread was done doing its job.