Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 528
user1019042 Avatar asked Nov 08 '16 15:11

user1019042


People also ask

How to get the claims of a user in ASP NET Core?

The ASP.NET Core client app only requires the profile scope. When using the id_token for claims, no extra claims mapping is required. Another way to get the user claims is to use the OpenID Connect User Info API.

What is an ASP NET claim?

Claims can be created from any user or identity data which can be issued using a trusted identity provider or ASP.NET Core identity. A claim is a name value pair that represents what the subject is, not what the subject can do. This article covers the following areas:

How do external authentication providers work in ASP NET Core?

An ASP.NET Core app can establish additional claims and tokens from external authentication providers, such as Facebook, Google, Microsoft, and Twitter. Each provider reveals different information about users on its platform, but the pattern for receiving and transforming user data into additional claims is the same.

How to add additional claims to the user's identity?

:) The quickest way to add some additional claims to the user's identity is to create your own implementation of IUserClaimsPrincipalFactory and register it in DI container. Here is the implementation of IUserClaimsPrincipalFactory which adds the value stored in ContactName property to the user's claims:


Video Answer


1 Answers

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

var claims = await _userManager.GetClaimsAsync(applicationUser);

See MSDN

like image 196
haim770 Avatar answered Nov 14 '22 23:11

haim770