【问题标题】:How to reset password with UserManager of ASP.NET MVC 5如何使用 ASP.NET MVC 5 的 UserManager 重置密码
【发布时间】:2014-04-26 08:16:20
【问题描述】:

我想知道是否有办法使用 ASP.NET MVC 5

UserManager 重置密码

我对已经有密码但没有成功的用户进行了尝试。有什么线索吗?

IdentityResult result = UserManager.AddPassword(forgotPasswordEvent.UserId.ToString(), model.ConfirmPassword);
if (result.Succeeded)
{
       //
}
else
{
        AddErrors(result);
}

【问题讨论】:

    标签: c# passwords asp.net-mvc-5 reset-password usermanager


    【解决方案1】:

    尝试使用用户存储:

     var user = UserManager.FindById(forgotPasswordEvent.UserId);
    
     UserStore<ApplicationUser> store = new UserStore<ApplicationUser>();
     store.SetPasswordHashAsync(user, uManager.PasswordHasher.HashPassword(model.ConfirmPassword));
    

    IdentityMembership 很酷,但仍然lacking some implementation

    更新

    Identity 2.0 现已推出并具有更多功能

    【讨论】:

    • 我试过你的样品,但后来我得到了TimeOutException。我仍在使用 Identity 1.0 并且我认为我无法迁移,因为它需要在我的应用程序中进行重大重构。我正在使用 Microsoft.Owin 2.10OWIN 1.0
    【解决方案2】:

    这里是ASP.NET Identity reset password

    UserManager<IdentityUser> userManager = 
        new UserManager<IdentityUser>(new UserStore<IdentityUser>());
    
    userManager.RemovePassword(userId);
    
    userManager.AddPassword(userId, newPassword);
    

    【讨论】:

    【解决方案3】:

    我想这是较新的,但 Identity 2.0 中有这样的 API:

    IdentityResult result = await UserManager.ResetPasswordAsync(user.Id, model.Code, model.Password);
    

    model.Code 是通过以下方式生成的,您应该将其作为电子邮件中的链接发送,以确保声称要更改密码的用户是拥有该电子邮件地址的用户:

    string code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
    

    【讨论】:

    • UserManager.ResetPasswordAsync(user.Id, UserManager.GeneratePasswordResetTokenAsync(user.Id), model.Password);
    • 重置代码是否保存在数据库中?当我们调用 resetpasswordasync 方法时它如何检查?
    【解决方案4】:

    我将此添加到我的 UserManager 类中:

        public virtual async Task<IdentityResult> UpdatePassword(ApplicationUser user, string newPassword)
        {
            var passwordStore = Store as IUserPasswordStore<ApplicationUser, string>;
    
            if (passwordStore == null)
                throw new Exception("UserManager store does not implement IUserPasswordStore");
    
            var result = await base.UpdatePassword(passwordStore, user, newPassword);
    
            if (result.Succeeded)
                result = await base.UpdateAsync(user);
    
            return result;
        }
    

    【讨论】:

    • 你这里的基类是什么?
    • 抱歉耽搁了,它的 UserManager。 ApplicationUser 派生自 IdentityUser。 UserManager 有一个属性“Store”,它(通常)是 IUserPasswordStore. 的一个实例
    • 谢谢,我已经想通了,但是我没有看到任何UpdatePassword 方法,请告诉我您使用的是哪个版本的 Identity 和 OWIN?
    【解决方案5】:

    试试这个代码。它工作得很好:

        var userStore = new UserStore<IdentityUser>();
    
        var userManager = new UserManager<IdentityUser>(userStore);
    
        string userName= UserName.Text;
    
        var user =userManager.FindByName(userName);
        if (user.PasswordHash != null  )
        {
            userManager.RemovePassword(user.Id);
        }
    
        userManager.AddPassword(user.Id, newpassword);
    

    【讨论】:

    • 欢迎来到 Stack Overflow!为了确保您的答案对尽可能广泛的受众有所帮助,您可以考虑对其进行修改以包括(1)评估现有代码的缺陷,(2)您的代码如何避免此类陷阱,以及(3)您的解决方案中的任何假设或缺陷。看看here 以获得灵感。再次感谢您发布答案,我希望将来能看到您的更多信息。
    • 这与当前接受的答案有何不同?
    • 旧答案没有说明如何获取用户并将其作为 user.id 传递,如下所示: string userName= UserName.Text; var user =userManager.FindByName(userName);如果密码为空值,它没有检查条件。
    • @GaneshPMP:这是一个有效的回复。因此,将其纳入您的答案中。
    【解决方案6】:

    在命名空间 Microsoft.AspNet.Identity 中有更改密码的扩展。

    https://msdn.microsoft.com/en-us/library/dn497466(v=vs.108).aspx

    【讨论】:

      【解决方案7】:
      var validPass= await userManager.PasswordValidator.ValidateAsync(txtPassword1.Text);
      if(validPass.Succeeded)
      {
          var user = userManager.FindByName(currentUser.LoginName);
          user.PasswordHash = userManager.PasswordHasher.HashPassword(txtPassword1.Text);
          var res= userManager.Update(user);
          if(res.Succeeded)
          {
              // change password has been succeeded
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2023-04-04
        • 2013-04-06
        • 1970-01-01
        • 2016-03-26
        • 2021-05-19
        • 2015-07-10
        • 1970-01-01
        • 2011-03-08
        • 2018-06-01
        相关资源
        最近更新 更多