示例#1
0
        public IActionResult Login(LoginViewModel model)
        {
            if (!ModelState.IsValid)
            {
                ModelState.AddModelError("", "Email & Password cannot be empty! Please valid your account");
                return(View(model));
            }


            var user = this._context.Users.FirstOrDefault(u => u.EmailAddress == model.EmailAddress);


            if (user != null)
            {
                var userRole = this._context.UserRoles.FirstOrDefault(ur => ur.UserId == user.Id);
                if (BCrypt.BCryptHelper.CheckPassword(model.Password, user.Password))
                {
                    if (user.LoginStatus == Infrastructures.Domain.Enums.LoginStatus.AccountLocked)
                    {
                        ModelState.AddModelError("", "Your account has been locked ");
                        return(View());
                    }
                    else if (user.LoginStatus == Infrastructures.Domain.Enums.LoginStatus.Inactive)
                    {
                        ModelState.AddModelError("", "Your account is In-active. Please verify your account.");
                        return(View(model));
                    }
                    else if (user.LoginRetries == 3 && user.Password != model.Password)
                    {
                        user.LoginRetries = user.LoginRetries + 1;
                        user.LoginStatus  = Infrastructures.Domain.Enums.LoginStatus.AccountLocked;
                        this._context.Users.Update(user);
                        this._context.SaveChanges();

                        ModelState.AddModelError("", "Your login is failed 4 times. Your account has been locked!.");
                        return(View(model));
                    }

                    else if (user.LoginRetries == 3 && user.Password == model.Password)
                    {
                        user.LoginStatus = Infrastructures.Domain.Enums.LoginStatus.NeedToChangepPassword;
                        this._context.Users.Update(user);
                        this._context.SaveChanges();

                        ModelState.AddModelError("", "Your login is failed more than " + user.LoginRetries + " times. your account need to change password if you failed one more the account has been locked!.");
                        return(View(model));
                    }
                    else if (userRole.Role == Infrastructures.Domain.Enums.Role.User && user.LoginStatus == Infrastructures.Domain.Enums.LoginStatus.Active && user.Password == model.Password)
                    {
                        user.LoginStatus  = Infrastructures.Domain.Enums.LoginStatus.Active;
                        user.LoginRetries = 0;
                        WebUser.GetUser(user.Id, user.FullName);
                        this._context.Users.Update(user);
                        this._context.SaveChanges();

                        return(Redirect("~/manage/authors/index"));
                    }
                    else
                    {
                        user.LoginRetries = user.LoginRetries + 1;

                        if (user.LoginRetries >= 3)
                        {
                            ModelState.AddModelError("", "Your account has been locked please contact an Administrator.");
                            user.LoginStatus = Infrastructures.Domain.Enums.LoginStatus.AccountLocked;
                        }

                        this._context.Users.Update(user);
                        this._context.SaveChanges();

                        ModelState.AddModelError("", "Invalid Login.");
                        return(View());
                    }
                }
                ModelState.AddModelError("", "Invalid Login.");
                return(View());
            }

            return(View(model));
        }