Ejemplo n.º 1
0
        public IActionResult Login([FromBody] Users model)
        {
            string token  = string.Empty;
            Users  result = new Users();

            try
            {
                ServiceAction action = new ServiceAction(Configuration);
                action.CheckUser(out result, model.Username);
                if (result == null)
                {
                    return(NotFound());
                }
                if (!Crypto.VerifyHashedPassword(result.Password, model.Password))
                {
                    return(Unauthorized());
                }

                //set jwt
                token = action.BuildToken(result);
                return(Ok(new { token = token, message = "login successfully" }));
            }
            catch (Exception ex)
            {
                _logger.LogError("Failed to execute POST");
                return(StatusCode(500, new { result = "", message = ex.Message }));
            }
        }
Ejemplo n.º 2
0
        public IActionResult Register([FromBody] Users model)
        {
            Users result = new Users();

            try
            {
                ServiceAction action = new ServiceAction(Configuration);
                action.CheckUser(out result, model.Username);
                if (result != null)
                {
                    return(BadRequest());
                }
                model.Password = Crypto.HashPassword(model.Password);
                action.Register(model);
                return(Ok(new { result = "ok", message = "register successfully" }));
            }
            catch (Exception ex)
            {
                _logger.LogError("Failed to execute POST");
                return(StatusCode(500, new { result = "", message = ex.Message }));
            }
        }