public async Task<JsonData> ChangePassword(ChangePasswordModel changePass) { try { var db = new DataContext(); var userMan = new UserManager<MyUser>(new UserStore<MyUser>(db)); userMan.UserValidator = new UserValidator<MyUser>(userMan) { AllowOnlyAlphanumericUserNames = false }; var user = await userMan.FindByIdAsync(User.Identity.GetUserId()); if (user == null) throw new Exception("please check your old password"); var newPassword = changePass.NewPassword; var result = await userMan.RemovePasswordAsync(user.Id); if (!result.Succeeded) throw new Exception(string.Join(", ", result.Errors)); var result2 = await userMan.AddPasswordAsync(user.Id, newPassword); if (!result2.Succeeded) throw new Exception(string.Join(", ", result2.Errors)); return DataHelpers.ReturnJsonData(null, true, "Password changed successful"); } catch (Exception e) { return DataHelpers.ExceptionProcessor(e); } }
public async Task<IdentityResult> ChangePassword(MyUser theUser, ChangePasswordModel model) { var db = new DataContext(); var userMan = new UserManager<MyUser>(new UserStore<MyUser>(db)); userMan.UserValidator = new UserValidator<MyUser>(userMan) { AllowOnlyAlphanumericUserNames = false }; //var theUser = await UserManager.FindByNameAsync(userName); var result = await userMan.ChangePasswordAsync(theUser.Id, model.OldPassword, model.NewPassword); return result; }