user1019042 user1019042 - 1 month ago 24
C# Question

how to get claims of another user using ASP.NET Core

I'm still learning identities with asp.net core. I'm doing a claims-based token authorization.
Most examples are about "Current" logged in user. In my case my RPC service is receiving a username & password of some user in the identity DB. I need to


  1. verify that a user with such credentials exist

  2. get all the claims of that user



so to verify if the user exists, I'm using this:

ApplicationUser applicationUser = await _userManager.FindByNameAsync(username);
bool exist = await _userManager.CheckPasswordAsync(applicationUser, password);
if (!exist)
{
// log and return
}


I don't know how to do the 2nd step properly. I guess I could do a simple linq to collect all user's claims, but I'm sure there is a better way using the identity methods.

Answer

You need to use the GetClaimsAsync() method. For example:

var claims = await _userManager.GetClaimsAsync(applicationUser);

See MSDN