예제 #1
0
 public JwtOutput CreateJwtToken(JwtInput input)
 {
     if (input == null)
     {
         throw new ArgumentNullException("input cannot null");
     }
     return(_jwt.GetJwtToken(input));
 }
        public async Task <IActionResult> Login([FromBody] LoginDto request)
        {
            var response  = new ApiResponse();
            var errorDict = new Dictionary <string, List <string> >();

            if (!ModelState.IsValid)
            {
                response.Errors = ModelState.GetErrors(_dynamicTypeFactory);
                return(BadRequest(response));
            }

            var user = _unitOfWork.Users.GetFirstOrDefault(u => u.Email == request.UserNameOrEmail || u.UserName == request.UserNameOrEmail);

            if (user == null || request.Password == null)
            {
                errorDict.Add(nameof(user), new List <string>()
                {
                    ResponseCodes.INVALID_USERNAME_OR_PASSWORD
                });
                response.Errors = errorDict.GetModelError(_dynamicTypeFactory);
                return(BadRequest(response));
            }

            var result = _signInManager.PasswordSignInAsync(user, request.Password, false, false).Result;

            if (result.IsLockedOut)
            {
                errorDict.Add(nameof(request.UserNameOrEmail), new List <string>()
                {
                    ResponseCodes.LOCK_OUT + user.LockoutEnd?.ToLocalTime().ToString("dd/MM/yyyy H:mm:ss zzz")
                });
                response.Errors = errorDict.GetModelError(_dynamicTypeFactory);
                return(BadRequest(response));
            }
            else if (!result.Succeeded)
            {
                await _signInManager.UserManager.AccessFailedAsync(user);

                errorDict.Add(nameof(request.UserNameOrEmail), new List <string>()
                {
                    ResponseCodes.INVALID_USERNAME_OR_PASSWORD
                });
                response.Errors = errorDict.GetModelError(_dynamicTypeFactory);
                return(BadRequest(response));
            }

            response.Data = _jwtProvider.GetJwtToken(user);
            return(Ok(response));
        }