public IHttpActionResult ChangePassword(ChangePassword cambioClave) { if (string.IsNullOrEmpty(cambioClave.Password)) throw new Exception("Ingrese la clave actual"); if (cambioClave.Password == cambioClave.NewPassword) throw new Exception("La clave nueva no debe ser igual a la anterior"); if (cambioClave.NewPassword != cambioClave.NewPasswordConfirm) throw new Exception("La clave nueva no coincide"); var bytesPwd = Convert.FromBase64String(cambioClave.NewPassword); var Pwd = Encoding.UTF8.GetString(bytesPwd); if (Pwd.Length < 4) throw new Exception("la nueva clave es muy corta"); using (var entities = new MoneyPoints_dlloEntities()) { var user = entities.Usuarios.FirstOrDefault(u => u.UsuarioId == cambioClave.UsuarioId && u.Password == cambioClave.Password); if (user == null) throw new Exception("Clave actual incorrecta"); user.Password = cambioClave.NewPassword; entities.Entry(user).State = System.Data.Entity.EntityState.Modified; entities.SaveChanges(); } return Ok(); }
public IHttpActionResult ChangePIN(ChangePassword cambioClave) { if (cambioClave.PIN == cambioClave.NewPIN) throw new Exception("El nuevo PIN no debe ser igual al anterior"); if (cambioClave.NewPIN != cambioClave.ConfirmPIN) throw new Exception("El nuevo PIN no coincide"); try { var bytesPIN = System.Convert.FromBase64String(cambioClave.NewPIN); var PIN = Encoding.UTF8.GetString(bytesPIN); if (PIN.Length != 4) throw new Exception("El PIN debe ser de cuatro digitos"); } catch (Exception) { throw new Exception("No se pudo decodificar la clave, debe estar en base 64"); } using (var entities = new MoneyPoints_dlloEntities()) { var user = entities.Usuarios.FirstOrDefault(u => u.UsuarioId == cambioClave.UsuarioId); if (user == null) throw new Exception("Usuario no existe"); var benef = user.Tercero.Beneficiarios.ToList()[0]; if (benef.Pin != cambioClave.PIN) throw new Exception("PIN incorrecto"); benef.Pin = cambioClave.NewPIN; entities.Entry(benef).State = System.Data.Entity.EntityState.Modified; entities.SaveChanges(); } return Ok(); }