ASP.NET Identity change password

前端 未结 10 1929
礼貌的吻别
礼貌的吻别 2020-11-29 18:09

I need ability to change password for user by admin. So, admin should not enter a current password of user, he should have ability to set a new password. I look at ChangePas

10条回答
  •  情话喂你
    2020-11-29 18:48

    This is just a refinement on the answer provided by @Tseng. (I had to tweak it to get it to work).

    public class AppUserManager : UserManager
    {
        .
        // standard methods...
        .
    
        public async Task ChangePasswordAsync(AppUser user, string newPassword)
        {
            if (user == null)
                throw new ArgumentNullException(nameof(user));
    
            var store = this.Store as IUserPasswordStore;
            if (store == null)
            {
                var errors = new string[] { "Current UserStore doesn't implement IUserPasswordStore" };
                return IdentityResult.Failed(errors);
            }
    
            var newPasswordHash = this.PasswordHasher.HashPassword(newPassword);
            await store.SetPasswordHashAsync(user, newPasswordHash);
            await store.UpdateAsync(user);
            return IdentityResult.Success;
        }
    }
    

    Note: this applies specifically to a modified setup that uses int as the primary keys for users and roles. I believe it would simply be a matter of removing the type args to get it to work with the default ASP.NET Identity setup.

提交回复
热议问题