简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM