Example #1
0
        public async Task <ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            var EmailConform = _dbhelpers.EmailConfirmation(model.LoginUsername);   //check if such mail confirmed
            var LogUserName  = _dbhelpers.FindUserName(model.LoginUsername);
            var userRole     = _dbhelpers.CheckUserRole(LogUserName);

            if (userRole == "Banned")
            {
                ModelState.AddModelError("", "User Banned.");
                return(View("Login"));
            }

            var result = await SignInManager.PasswordSignInAsync(model.LoginUsername, model.LoginPassword, model.RememberMe, shouldLockout : false);

            //check if email verified & user exist you can login
            if (EmailConform == false && LogUserName != null && result.ToString() == "Success")
            {
                AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
                //UserEmailConformation = model.LoginUsername;
                return(RedirectToAction("EmailConfirmationFailed", "Account"));
            }
            else
            {
                ViewBag.ReturnUrl = returnUrl;
                if (ModelState.IsValid)
                {
                    switch (result)
                    {
                    case SignInStatus.Success:
                        _dbhelpers.UpdateLastLoginDate(model.LoginUsername);    //update last login
                        _dbhelpers.UpdateOnlineStatus(model.LoginUsername);
                        return(RedirectToLocal(returnUrl));

                    case SignInStatus.LockedOut:      //user bloked? user is lockout
                        return(View("Lockout"));

                    case SignInStatus.RequiresVerification:     //two factor
                        return(RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe }));

                    case SignInStatus.Failure:
                    default:
                        ModelState.AddModelError("", "Invalid login attempt. Or User and password do not match");
                        return(View("Login"));
                    }
                }
                // If we got this far, something failed, redisplay form
                return(View("Login"));
            }
        }