简体   繁体   中英

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.

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

在此处输入图像描述 You can read full article, and get info about all userManager methods here

You can use FindBy...() methods to find users by parameters.

FindByEmailAsync() gets the user, if any, associated with the normalized value of the specified email address.

FindByEmailAsync() finds and returns a user, if any, who has the specified userId .

FindByLoginAsync() retrieves the user associated with the specified external login provider and login provider key.

FindByNameAsync() finds and returns a user, if any, who has the specified user name.

var user = await userManager.FindByEmailAsync(email);
var user2 = await userManager.FindByIdAsync(id);

var loginProvider = "Github";
var providerKey = "Stian";
var user3 = await userManager.FindByLoginAsync(loginProvider, providerKey);

var user4 = await userManager.FindByNameAsync(userName);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM