Beispiel #1
0
        //----------------------------------------------------------------------

        public async Task <bool> CambiarPassword(UsuarioChangePwVM model)
        {
            if (model.usuario_id <= 0)
            {
                throw new Exception("El ID de este usuario es invalido");
            }

            if (model.password != model.passwordConfirm)
            {
                throw new Exception("El password no coincide con el de confirmacion");
            }

            var usuario = await _context.Usuarios.FindAsync(model.usuario_id);

            if (usuario == null)
            {
                throw new Exception("No existe un usuario con esa ID");
            }

            bool okPwOld = ChkPassword(model.passwordold, usuario);

            if (!okPwOld)
            {
                throw new Exception("Este password no coincide con el anterior");
            }

            CrearPasswordHash(model.password, out byte[] passwordHash, out byte[] passwordSalt);
            usuario.password_hash = passwordHash;
            usuario.password_salt = passwordSalt;

            await _context.SaveChangesAsync();

            return(true);
        }
Beispiel #2
0
        public async Task <IActionResult> CambiarPw([FromBody] UsuarioChangePwVM model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (model.usuario_id <= 0)
            {
                return(BadRequest());
            }

            string Idstr = HttpContext.GetClaim("usuarioId");

            if (Idstr.EsNulaOVacia())
            {
                throw new Exception("No existe el Id del Usuario");
            }
            int IdUSer = int.Parse(Idstr);

            if (IdUSer != model.usuario_id)
            {
                throw new Exception("El Id del Usuario no coincide con el usuario conectado");
            }

            if (model.password != model.passwordConfirm)
            {
                return(BadRequest("Los password no coinciden"));
            }

            var user = await _context.Usuarios.FirstOrDefaultAsync(x => x.usuario_id == model.usuario_id);

            if (user == null)
            {
                return(NotFound());
            }

            bool okPwOld = _usuarioService.ChkPassword(model.passwordold, user);

            if (okPwOld == false)
            {
                return(BadRequest("El Password anterior no coincide"));
            }

            _usuarioService.CrearPasswordHash(model.password, out byte[] passwordHash, out byte[] passwordSalt);
            user.password_hash = passwordHash;
            user.password_salt = passwordSalt;


            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                // guardar el error
                return(BadRequest());
            }

            return(Ok());
        }