Esempio n. 1
0
        public async Task <IActionResult> PostLogin(LoginModel model)
        {
            if (ModelState.IsValid)
            {
                UserLoginResult loginResult = await _userRegistrationService.ValidateUser(model.UserName, model.Password);

                switch (loginResult)
                {
                case UserLoginResult.Successful:
                    User user = await _userRegistrationService.GetUserByUserName(model.UserName);

                    AuthenticationResult result = await _authenticationService.AuthenticateUser(user);

                    if (result.Success)
                    {
                        return(Ok(model.PrepareLoginSuccessModel(result)));
                    }
                    ModelState.AddModelError(string.Empty, "Login Failed");
                    break;

                case UserLoginResult.UserNotExist:
                    ModelState.AddModelError(string.Empty, "User not exist");
                    break;

                case UserLoginResult.WrongPassword:
                    ModelState.AddModelError(string.Empty, "Wrong Credential");
                    break;

                case UserLoginResult.NotActive:
                    ModelState.AddModelError(string.Empty, "User Inactive");
                    break;

                case UserLoginResult.LockedOut:
                    ModelState.AddModelError(string.Empty, "Profile Locked");
                    break;
                }
            }
            return(BadRequest(ModelState.Select(x => x.Value.Errors.First().ErrorMessage)));
        }