public async Task <ActionResult <ViewModelResponse> > ChangePassword([FromBody] ViewModelPassword model, int id, string token) { try { // verificar token de solicitud var pr = _dbContext.PasswordReminder.FirstOrDefault(x => x.Id == id && x.Token.Equals(new Guid(token))); if (pr != null) { if (DateTime.Now.CompareTo(pr.ExpiresAt) < 0) { if (!model.NewPassword.Equals(model.ConfirmedNewPassword)) { return(Json(new ViewModelResponse() { Error = true, Response = "Las contraseñas no coinciden." })); } var user = await _userManager.FindByIdAsync(pr.IdUser.ToString()); if (user != null) { var result = await _userManager.ChangePasswordAsync(user, model.Password, model.NewPassword); if (result.Succeeded) { var aLdap = await ServiceLDAP.ModifyAsync(user.UserName, user.UserName, model.NewPassword, user.Name, user.LastName, user.Email); if (aLdap) { _dbContext.PasswordReminder.Remove(pr); _dbContext.SaveChanges(); return(Json(new ViewModelResponse() { Error = false, Response = "Contraseña modificada exitosamente." })); } else { return(Json(new ViewModelResponse() { Error = true, Response = "Ocurrio un error" })); } } else { string error = string.Empty; foreach (var e in result.Errors) { error += "{" + e.Code + "}-" + e.Description + Environment.NewLine; } return(Json(new ViewModelResponse() { Error = true, Response = error })); } } return(Json(new ViewModelResponse() { Error = true, Response = "Usuario no encontrado" })); } return(Json(new ViewModelResponse() { Error = true, Response = "Token de cambio de contraseña ya expiró, solicite uno nuevo." })); } return(Json(new ViewModelResponse() { Error = true, Response = "Token de cambio de contraseña no encontrado o éste ya espiró." })); } catch (Exception e) { return(Json(new ViewModelResponse() { Error = true, Response = String.Format("Ocurrio un error al intentar verificar el correo electrónico, intenta nueva mente. {0}", e.Message) })); } }
public async Task <ActionResult <ViewModelResponse> > ChangePasswordUser([FromBody] ViewModelPassword model, string id) { try { var user = await _userManager.FindByIdAsync(id); if (user != null) { if (!model.NewPassword.Equals(model.ConfirmedNewPassword)) { return(StatusCode(StatusCodes.Status400BadRequest, new ViewModelResponse() { Error = true, Response = "Las contraseñas no coinciden." })); } var result = await _userManager.ChangePasswordAsync(user, model.Password, model.NewPassword); if (result.Succeeded) { var aLdap = await ServiceLDAP.ModifyAsync(user.UserName, user.UserName, model.NewPassword, user.Name, user.LastName, user.Email); if (aLdap) { return(Json(new ViewModelResponse() { Error = false, Response = "Contraseña modificada exitosamente." })); } else { return(Json(new ViewModelResponse() { Error = true, Response = "Ocurrio un error" })); } } else { string error = string.Empty; foreach (var e in result.Errors) { error += "{" + e.Code + "}-" + e.Description + Environment.NewLine; } return(Json(new ViewModelResponse() { Error = true, Response = error })); } } return(Json(new ViewModelResponse() { Error = true, Response = "El usuario no existe" })); } catch (Exception e) { return(StatusCode(StatusCodes.Status500InternalServerError, new ViewModelResponse() { Error = true, Response = String.Format("Ocurrio un error al intentar verificar el correo electrónico, intenta nueva mente. {0}", e.Message) })); } }