Exemple #1
0
        public async Task <IActionResult> Authenticate([FromBody] LoginModel login)
        {
            try
            {
                var token = await _authenticateService.AuthenticateAsync(login.Email, login.Password);

                return(Ok(token));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
        //  public async Task<IActionResult> LoginTokenFromService([FromBody] UserRequestDto model)
        public async Task <JsonResult> LoginTokenFromService([FromBody] UserRequestDto model)
        {
            if (ModelState.IsValid)
            {
                //if (this.User.Identity.IsAuthenticated)
                try
                {
                    var token = await _authenticateService.AuthenticateAsync(model);

                    if (token != null)
                    {
                        UserDataResponseDto userData = new UserDataResponseDto()
                        {
                            UserName = model.userName,
                            Token    = token
                                       // ,ExpirationDate = string.Empty Then replace value if this property is used
                        };

                        //return Ok(token);
                        // return new JsonResult(token);
                        return(new JsonResult(userData));
                    }
                }
                catch (Exception e)
                {
                    string inner = e.InnerException != null ? e.InnerException.Message : string.Empty;
                    string error = "ERROR Mesagge:" + e.Message + "||||Inner" + inner;
                    error += "|||Stactrace:" + e.StackTrace;
                    //throw;

                    UserDataResponseDto userData = new UserDataResponseDto()
                    {
                        UserName = model.userName,
                        Token    = error
                                   //, ExpirationDate = string.EmptyThen replace value if this property is used
                    };


                    return(new JsonResult(userData));
                }
            }

            _fullErrorList = "Clave o usuario incorrecto"; //TODO: en realidad se deberia hacer un refactor para que se devuelva el error del sistema ahora que esta traducido
            ModelState.AddModelError("message", _fullErrorList);
            //return BadRequest("invalid login");
            return(null);
            // return BadRequest(ModelState); va este pero hay que cambiar la respuesta del JSON
        }
        public async Task <IActionResult> SignInAsync([FromBody] AuthenticationDTO credentials)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var result = await _authenticateService.AuthenticateAsync(credentials);

            if (result.Result == AuthenticationResults.Succes)
            {
                return(Ok(result.TokenString));
            }
            else
            {
                return(BadRequest());
            }
        }