0

I'm using Microsoft Identity 3. I can access the claims of the current user. But I can't figure out how to do the same for another user.

For the current user, inside the controller, I can access the claims collection via:

IEnumerable<Claim> claims = User.Claims;

I can see all the user claims and I can add a claim as follows:

var user = await GetCurrentUserAsync();
await _userManager.AddClaimAsync(user, new Claim("role", "manager"));

But if I do this:

IdentityUser user = await _userManager.FindByIdAsync(userid);

"user" has a "Claims" collection but the count is zero and the collection is empty. How can I access the claims of other than the current user and be able to add and delete claims?

John Pankowicz
  • 4,203
  • 2
  • 29
  • 47

1 Answers1

1

The solution to what I wanted to do turned out to be very easy -- even though I don't fully understand the "why". I can just up-cast IdentityUser to ApplicationUser in the following and it works:

IdentityUser user = await _userManager.FindByIdAsync(userId);
// The following will show the current claims:
var claims = await _userManager.GetClaimsAsync((ApplicationUser) user);
// The following adds a new claim:
await _userManager.AddClaimAsync((ApplicationUser) user, new Claim("time", "yesterday"));
John Pankowicz
  • 4,203
  • 2
  • 29
  • 47