예제 #1
0
 /// <summary>
 /// Login for the given username and password.
 /// </summary>
 /// <returns>The authorized user if login is successful.</returns>
 /// <param name="pUsername">Username.</param>
 /// <param name="pPassword">Password.</param>
 public ResponseDTO <UserDTO> Login(string pUsername, string pPassword)
 {
     using (IUnitOfWork bUoW = _unitOfWorkFactory.GetUnitOfWork())
     {
         User user = bUoW.UserRepository.Get(pUsername);
         // Non-existing user
         if (user == null)
         {
             return(ResponseDTO <UserDTO> .NotFound($"There is no user associated with the username {pUsername}"));
         }
         // Password matches with user's password: successful login
         if (string.Equals(user.Password, pPassword))
         {
             return(ResponseDTO <UserDTO> .Ok(_mapper.Map <UserDTO>(user)));
         }
         return(ResponseDTO <UserDTO> .Unauthorized("Incorrect password."));
     }
 }
        public async Task <IActionResult> Login(CustomerLoginDto dto)
        {
            var customer = await _repository.Login(dto.Username, dto.Password);

            if (customer == null)
            {
                return(BadRequest(ResponseDTO.Unauthorized()));
            }

            var claims = new[]
            {
                new Claim("sub", customer.Username)
            };

            var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config.GetSection("AppSettings:Token").Value));

            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature);

            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject            = new ClaimsIdentity(claims),
                Expires            = DateTime.Now.AddDays(1),
                SigningCredentials = creds,
            };

            var tokenHandler = new JwtSecurityTokenHandler();

            var token       = tokenHandler.CreateToken(tokenDescriptor);
            var tokenString = tokenHandler.WriteToken(token);

            var data = new ResponseLoginDto
            {
                id              = customer.Id,
                name            = customer.Name,
                dateOfBirth     = customer.DateOfBirth,
                email           = customer.Email,
                phoneNumber     = customer.PhoneNumber,
                gender          = customer.Gender,
                authorizedToken = tokenString
            };

            return(Ok(new ResponseDTO(data, "200", "Login successfully")));
        }
예제 #3
0
        public async Task <IActionResult> Login(UserForLoginDto userForLoginDto)
        {
            var userFromRepo = await repo.Login(userForLoginDto.Username, userForLoginDto.Password);

            if (userFromRepo == null)
            {
                return(BadRequest(ResponseDTO.Unauthorized()));
            }

            var role = await context.Roles.FirstOrDefaultAsync(x => x.Id == userFromRepo.RoleId);

            var claims = new[]
            {
                new Claim(ClaimTypes.NameIdentifier, userFromRepo.Id.ToString()),
                new Claim(ClaimTypes.Name, userFromRepo.Username),
                new Claim(ClaimTypes.Role, role.Name)
            };

            var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(config.GetSection("AppSettings:Token").Value));

            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha512Signature);

            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject            = new ClaimsIdentity(claims),
                Expires            = DateTime.Now.AddDays(1),
                SigningCredentials = creds
            };

            var tokenHandler = new JwtSecurityTokenHandler();

            var token = tokenHandler.CreateToken(tokenDescriptor);

            return(Ok(new
            {
                token = tokenHandler.WriteToken(token)
            }));
        }
예제 #4
0
        /// <summary>
        /// Starts new session.
        /// </summary>
        /// <returns>The first question of the created session.</returns>
        /// <param name="pCategory">Category.</param>
        /// <param name="pLevel">Level.</param>
        /// <param name="pQuestionsQuantity">Questions quantity.</param>
        public ResponseDTO <SessionDTO> StartNewSession(CategoryDTO pCategory, LevelDTO pLevel, int pQuestionsQuantity)
        {
            if (LoggedUser == null)
            {
                return(ResponseDTO <SessionDTO> .Unauthorized("You must log in before starting a new session."));
            }
            if (pCategory == null)
            {
                return(ResponseDTO <SessionDTO> .BadRequest("Select a category."));
            }
            if (pLevel == null)
            {
                return(ResponseDTO <SessionDTO> .BadRequest("Select a level."));
            }
            var response = _operativeService.NewSession(LoggedUser.Id, pCategory.Id, pLevel.Id, pQuestionsQuantity);

            if (response.Success)
            {
                CurrentSession = response.Data;
                CurrentSession.RemainingQuestions = CurrentSession.Questions.ToList();
            }
            return(response);
        }