Search code examples
c#asp.net-core-identity

How can I get a user (not the currently logged in user) by Id, name, email or other search criteria, using UserManager?


This should be the simplest thing in the world to do, but I can't find any information about it.

As a temporary solution, I have resorted to this:

ApplicationUser user = await db.Users.Where(u => u.Email == email).FirstOrDefaultAsync();

But what I originally imagined I could do was something like this:

await ApplicationUser user = userManager.GetUserAsync(email);

I already have access to userManager in my controller.


Solution

  • ApplicationUser firstUser = await userManager.FindByEmailAsync(email)
    ApplicationUser secondUser = await userManager.FindByIdAsync(id)
    ApplicationUser thirdUser= await userManager.FindByNameAsync(name)
    

    enter image description here You can read full article, and get info about all userManager methods here