示例#1
0
        public async Task <ActionResult> Logout()
        {
            try
            {
                string token = HttpContext.Request.Headers["authorization"].Single().Split(" ")[1];
                if (!(await _repository.ValidateToken(token)))
                {
                    return(Unauthorized());
                }

                InvalidToken  invalidToken = new InvalidToken();
                var           identity     = HttpContext.User.Identity as ClaimsIdentity;
                IList <Claim> claim        = identity.Claims.ToList();

                DateTime expirationDate = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
                expirationDate = expirationDate.AddSeconds(Double.Parse(claim[3].Value)).ToLocalTime();

                invalidToken.userId         = Int32.Parse(claim[0].Value);
                invalidToken.expirationDate = expirationDate;
                invalidToken.token          = token;

                await _repository.CreateInvalidToken(invalidToken);

                await _repository.SaveChanges();

                return(Ok());
            }
            catch (Exception)
            {
                return(new StatusCodeResult(StatusCodes.Status500InternalServerError));
            }
        }
示例#2
0
        public async Task <ActionResult <UserTaskReadDTO> > CreateTask(UserTaskCreateDTO taskCreateDTO)
        {
            try
            {
                string token = HttpContext.Request.Headers["authorization"].Single().Split(" ")[1];
                if (!await _repository.ValidateToken(token))
                {
                    return(Unauthorized());
                }

                string loggedUserId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                taskCreateDTO.userId = Int32.Parse(loggedUserId);

                if (!await _repository.IsAdvancedUser(taskCreateDTO.userId))
                {
                    taskCreateDTO.text = null;
                }

                var taskModel = _mapper.Map <UserTask>(taskCreateDTO);
                await _repository.CreateTask(taskModel);

                await _repository.SaveChanges();

                var taskReadDTO = _mapper.Map <UserTaskReadDTO>(taskModel);

                return(CreatedAtRoute(nameof(GetTaskById), new { id = taskReadDTO.id }, taskReadDTO));
            }
            catch (Exception)
            {
                return(new StatusCodeResult(StatusCodes.Status500InternalServerError));
            }
        }
示例#3
0
        public async Task <ActionResult <UserReadDTO> > CreateUser(UserCreateDTO userCreateDTO)
        {
            try
            {
                var hashedPassword = BCrypt.Net.BCrypt.HashPassword(userCreateDTO.password, 10);
                userCreateDTO.password = hashedPassword;
                var userModel = _mapper.Map <User>(userCreateDTO);
                try
                {
                    await _repository.CreateUser(userModel);
                }
                catch (Exception e)
                {
                    if ("Email alredy exists!".Equals(e.Message))
                    {
                        return(BadRequest(new { error = "Email alredy exists!" }));
                    }
                    throw e;
                }
                await _repository.SaveChanges();

                var userReadDTO = _mapper.Map <UserReadDTO>(userModel);
                return(CreatedAtRoute(nameof(CreateUser), userReadDTO));
            }
            catch (Exception)
            {
                return(new StatusCodeResult(StatusCodes.Status500InternalServerError));
            }
        }