Changing password asp.net identity

2019-02-28 00:29发布

问题:

Is there a quick way to verify is user exists and based on existence of user name get user id?

(i.e. the user only has the user name and the adds it to change their password)?

I would think something like this... String userId = User.Identity.GetUserId(userName);

I am looking for a quick way for local users to change their password if need be.

回答1:

User.Identity.GetUserId() will return the id of current logged in user. From UserManager you can get any user from their username like this

var user = UserManager.FindByName("the username here");

then you can change password from UserManager again

UserManager.ChangePassword(user.Id, "OldPassword", "NewPassword");


回答2:

Have it, thanks

        var user = UserManager.FindByName(userName);
        String userId = user.Id;