在开发ASP.NET WEB APP的时候,时间长了容易忘记最初设置的密码,即便打开数据库也很差重置,由于密码都是加密存储在数据库中的。下面是几种经过代码重置密码的方式:数据库
第一种:加密
string resetToken = await UserManager.GeneratePasswordResetTokenAsync(model.Id); IdentityResult passwordChangeResult = await UserManager.ResetPasswordAsync(model.Id, resetToken, model.NewPassword);
第二种:spa
UserManager<IdentityUser> userManager = new UserManager<IdentityUser>(new UserStore<IdentityUser>()); userManager.RemovePassword(userId); userManager.AddPassword(userId, newPassword);
第三种:code
ApplicationDbContext context = new ApplicationDbContext(); UserStore<ApplicationUser> store = new UserStore<ApplicationUser>(context); UserManager<ApplicationUser> UserManager = new UserManager<ApplicationUser>(store); String userId = User.Identity.GetUserId();//"<YourLogicAssignsRequestedUserId>"; String newPassword = "test@123"; //"<PasswordAsTypedByUser>"; String hashedNewPassword = UserManager.PasswordHasher.HashPassword(newPassword); ApplicationUser cUser = await store.FindByIdAsync(userId); await store.SetPasswordHashAsync(cUser, hashedNewPassword); await store.UpdateAsync(cUser);