简体   繁体   中英

How to get IdentityUser by Username

I have previously worked with Membership through "System.Web.Security.Membership"

Here, you can do the following:

var currentUser = Membership.GetUser();
var otherUser = Membership.GetUser(username);

...giving you a MembershipUser.

Now, with Identity, I can find a load of ways to get the current logged in user . But no way to get another user.

I can use:

var userStore = new UserStore<IdentityUser>();
var userManager = new UserManager<IdentityUser>(userStore);
var user = userManager.Find(username, password);

But that takes both username and password, with no overload for just username.

How do i get the IdentityUser from only a username?

Almost every answer I find is connected to MVC. This is for a WCF service, where authorization is made using Identity. And in some cases the user is getting to the site from an other site with a generated "token" - an encrypted string, containing the username. From here, user is logged in and a session-cookie is set, depending on users settings.

Also, is there a shorter way to get UserInformation?

"var currentUser = Membership.GetUser(username);"

is much more convenient than

"var user2 = (new UserManager((new UserStore()))).Find(username, password);"

UserManager has UserManager<TUser>.FindByNameAsync method. You can try using it to find user by name.

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