ASP.NET Identity reset password

前端 未结 10 1120
无人及你
无人及你 2020-12-02 05:09

How can I get the password of a user in the new ASP.NET Identity system? Or how can I reset without knowing the current one (user forgot password)?

10条回答
  •  误落风尘
    2020-12-02 05:33

    In current release

    Assuming you have handled the verification of the request to reset the forgotten password, use following code as a sample code steps.

    ApplicationDbContext =new ApplicationDbContext()
    String userId = "";
    String newPassword = "";
    ApplicationUser cUser = UserManager.FindById(userId);
    String hashedNewPassword = UserManager.PasswordHasher.HashPassword(newPassword);
    UserStore store = new UserStore();            
    store.SetPasswordHashAsync(cUser, hashedNewPassword);
    

    In AspNet Nightly Build

    The framework is updated to work with Token for handling requests like ForgetPassword. Once in release, simple code guidance is expected.

    Update:

    This update is just to provide more clear steps.

    ApplicationDbContext context = new ApplicationDbContext();
    UserStore store = new UserStore(context);
    UserManager UserManager = new UserManager(store);
    String userId = User.Identity.GetUserId();//"";
    String newPassword = "test@123"; //"";
    String hashedNewPassword = UserManager.PasswordHasher.HashPassword(newPassword);                    
    ApplicationUser cUser = await store.FindByIdAsync(userId);
    await store.SetPasswordHashAsync(cUser, hashedNewPassword);
    await store.UpdateAsync(cUser);
    

提交回复
热议问题