Ask Your Question
1

How can one create a filter expression in ASP.NET Core 6 that retrieves users with particular claims?

asked 2022-11-11 11:00:00 +0000

scrum gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-02-08 02:00:00 +0000

plato gravatar image

To retrieve users with particular claims in ASP.NET Core 6, you can use the following filter expression:

var usersWithClaim = await userManager.Users
    .Where(u => u.Claims.Any(c => c.ClaimType == "claimType" && c.ClaimValue == "claimValue"))
    .ToListAsync();

This filter expression retrieves all users from the user manager that have at least one claim matching the specified claim type and claim value.

You can replace "claimType" and "claimValue" with the actual claim type and value that you want to search for.

Note that you will need to inject the user manager into your controller or service using dependency injection. Also, make sure to import the necessary namespaces, such as Microsoft.AspNetCore.Identity and System.Linq.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-11-11 11:00:00 +0000

Seen: 8 times

Last updated: Feb 08 '23