示例#1
0
 public IActionResult Login([FromBody] RequestLogin req)
 {
     try {
         // Request validation
         if (req == null || req.User == null || req.Password == null)
         {
             Console.WriteLine("[Login] -> Faltan datos requeridos para login");
             RestResponse responseErr = RestUtils.GenerateResponseErrorWith(
                 new ResponseError(
                     RestUtils.RESPONSE_BADREQUEST_CODE,
                     "Faltan datos requeridos para login"
                     )
                 );
             responseErr.Header.Message = RestUtils.RESPONSE_BADREQUEST_MSG;
             return(BadRequest(responseErr));
         }
         Console.WriteLine("[Login] -> Iniciando login para user: "******"Fallo autenticacion de usuario";
             Console.WriteLine("[Login] -> " + response.Header.Message);
             return(Unauthorized(response));
         }
         else
         {
             response = RestUtils.GenerateResponseOkWithData(result);
             Console.WriteLine("[Login] -> " + response.Header.Message);
             string tokenString = this.generateToken(result);
             Console.WriteLine("[Login] -> token generated");
             response.Data.Add(new { token = tokenString });
             return(Ok(response));
         }
     } catch (Exception exception) {
         RestResponse response = RestUtils.GenerateResponseErrorWith(
             new ResponseError(
                 exception.Message,
                 exception.GetType().ToString()
                 )
             );
         // errores generales
         Console.WriteLine("[Login] ->" + RestUtils.RESPONSE_INTERNAL_ERROR_MSG);
         response.Header.Message = RestUtils.RESPONSE_INTERNAL_ERROR_MSG;
         return(StatusCode(
                    StatusCodes.Status500InternalServerError,
                    response
                    ));
     }
 }