Beispiel #1
0
        public IActionResult Login(User user)
        {
            ModelState.Clear();
            ViewBag.login = true;
            var allUsers = _userDataAccess.GetAllUsers();
            var userId   = _userDataAccess.GetUserId(user);

            user.Id = userId;
            var userInfo = _userDataAccess.GetUserById(user.Id);

            if (!allUsers.Any(x => x.Email == user.Email))
            {
                ModelState.AddModelError(string.Empty, "User with this email doesn't exists!");
            }
            else if (!userInfo.ConfirmAccount)
            {
                ModelState.AddModelError(string.Empty, "We have send an email to your email account, please confirm it!");
            }
            else if (ModelState.IsValid)
            {
                var userEnteredPass = HashPassword.ComparePasswordAndHash(user.Password, userInfo.Salt, userInfo.Password);

                if (!userEnteredPass)
                {
                    ModelState.AddModelError("WrongPassword", "Please try again!");
                }
                else
                {
                    try
                    {
                        _userDataAccess.SignIn(HttpContext, user.Id);
                        return(RedirectToAction("Dashboard", "Studio"));
                    }
                    catch (Exception ex)
                    {
                        ModelState.AddModelError("summary", ex.Message);
                        return(View("Views/Account/RegisterLogin.cshtml"));
                    }
                }
            }
            return(View("Views/Account/RegisterLogin.cshtml"));
        }