Exemplo n.º 1
0
        public IHttpActionResult SignIn([FromBody] SignInRequest credentials)
        {
            IHttpActionResult result = null;

            try
            {
                if (credentials != null)
                {
                    if (string.IsNullOrWhiteSpace(credentials.Username))
                    {
                        throw new Exception($"Debe ingresar el nombre de usuario.");
                    }

                    if (string.IsNullOrWhiteSpace(credentials.Password))
                    {
                        throw new Exception($"Debe ingresar la contraseña.");
                    }

                    credentials.Password = EncryptHelper.SHA256(credentials.Password);

                    User user = authService.SignIn(context, credentials.Username, credentials.Password);
                    if (user != null)
                    {
                        result = Ok(user.Map(true));
                    }
                    else
                    {
                        throw new Exception($"Credenciales incorrectas.");
                    }
                }
            }
            catch (Exception ex)
            {
                result = BadRequest(ex.Message);
            }

            return(result);
        }