public IActionResult Index(string returnUrl)
        {
            // has the user been remembered?
            if (ValidateRememberedLogin())
            {
                if (String.IsNullOrEmpty(returnUrl))
                {
                    return(Redirect(_settings.LoginSuccessUrl));
                }
                else
                {
                    return(Redirect(returnUrl));
                }
            }

            LoginViewModel model = new LoginViewModel(GetModelData(),
                                                      String.IsNullOrEmpty(returnUrl) ? _settings.LoginSuccessUrl : returnUrl,
                                                      _settings.ShowRememberMe);


            LoginCacheItem loginCacheItem = GetCachedLoginAttempt(false);

            if (loginCacheItem != null)
            {
                model.ShowCaptchaImage     = loginCacheItem.LoginAttempts >= _settings.CaptchaShowFailCount;
                loginCacheItem.CaptchaText = GetRandomWord(_settings.CaptchaWordLength, CaptchaCharacters);
            }

            return(View(model));
        }
        public ActionResult GetCaptchaImage()
        {
            LoginCacheItem loginCacheItem = GetCachedLoginAttempt(false);

            if (loginCacheItem == null)
            {
                return(StatusCode(400));
            }

            CaptchaImage ci = new CaptchaImage(loginCacheItem.CaptchaText, 240, 60, "Century Schoolbook");

            try
            {
                // Write the image to the response stream in JPEG format.
                using (MemoryStream ms = new MemoryStream())
                {
                    ci.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

                    return(File(ms.ToArray(), "image/png"));
                }
            }
            catch (Exception err)
            {
                if (!err.Message.Contains("Specified method is not supported."))
                {
                    throw;
                }
            }
            finally
            {
                ci.Dispose();
            }

            return(null);
        }
        public IActionResult ForgotPassword(ForgotPasswordViewModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            LoginCacheItem loginCacheItem = GetCachedLoginAttempt(true);

            if (!String.IsNullOrEmpty(loginCacheItem.CaptchaText))
            {
                if (!loginCacheItem.CaptchaText.Equals(model.CaptchaText))
                {
                    ModelState.AddModelError(String.Empty, Languages.LanguageStrings.CodeNotValid);
                }
            }

            if (ModelState.IsValid && _loginProvider.ForgottenPassword(model.Username))
            {
                RemoveLoginAttempt();
                return(Redirect("/Login/"));
            }

            ModelState.AddModelError(String.Empty, Languages.LanguageStrings.InvalidUsernameOrPassword);

            loginCacheItem.CaptchaText = GetRandomWord(_settings.CaptchaWordLength, CaptchaCharacters);
            model.CaptchaText          = loginCacheItem.CaptchaText;
            model.Breadcrumbs          = GetBreadcrumbs();
            model.CartSummary          = GetCartSummary();

            return(View(model));
        }
        public IActionResult ForgotPassword(ForgotPasswordViewModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            LoginCacheItem loginCacheItem = GetCachedLoginAttempt(true);

            if (!String.IsNullOrEmpty(loginCacheItem.CaptchaText))
            {
                if (!loginCacheItem.CaptchaText.Equals(model.CaptchaText))
                {
                    ModelState.AddModelError(String.Empty, "Invalid Validation Code");
                }
            }

            if (ModelState.IsValid && _loginProvider.ForgottenPassword(model.Username))
            {
                RemoveLoginAttempt();
                return(Redirect("/Login/"));
            }

            ModelState.AddModelError(String.Empty, "The details you provided could not be validated.");

            loginCacheItem.CaptchaText = GetRandomWord(_settings.CaptchaWordLength, CaptchaCharacters);
            model.CaptchaText          = loginCacheItem.CaptchaText;

            return(View(model));
        }
        public IActionResult Index(LoginViewModel model)
        {
            LoginCacheItem loginCacheItem = GetCachedLoginAttempt(true);

            if (!String.IsNullOrEmpty(loginCacheItem.CaptchaText))
            {
                if (!loginCacheItem.CaptchaText.Equals(model.CaptchaText))
                {
                    ModelState.AddModelError(String.Empty, "Invalid Validation Code");
                }
            }

            loginCacheItem.LoginAttempts++;

            model.ShowCaptchaImage = loginCacheItem.LoginAttempts >= _settings.CaptchaShowFailCount;

            UserLoginDetails loginDetails = new UserLoginDetails();

            switch (_loginProvider.Login(model.Username, model.Password, GetIpAddress(),
                                         loginCacheItem.LoginAttempts, ref loginDetails))
            {
            case LoginResult.Success:
                RemoveLoginAttempt();

                UserSession session = GetUserSession();

                if (session != null)
                {
                    session.Login(loginDetails.UserId, loginDetails.Username, loginDetails.Email);
                }

                if (model.RememberMe)
                {
                    CookieAdd(_settings.RememberMeCookieName, Encrypt(loginDetails.UserId.ToString(), _settings.EncryptionKey), _settings.LoginDays);
                }

                return(Redirect(model.ReturnUrl));

            case LoginResult.AccountLocked:
                return(RedirectToAction("AccountLocked", new { username = model.Username }));

            case LoginResult.PasswordChangeRequired:
                return(Redirect(_settings.ChangePasswordUrl));

            case LoginResult.InvalidCredentials:
                ModelState.AddModelError(String.Empty, "Invalid username or password");
                break;
            }

            if (model.ShowCaptchaImage)
            {
                loginCacheItem.CaptchaText = GetRandomWord(_settings.CaptchaWordLength, CaptchaCharacters);
            }

            return(View(model));
        }
        public IActionResult ForgotPassword()
        {
            ForgotPasswordViewModel model = new ForgotPasswordViewModel(GetModelData());

            LoginCacheItem loginCacheItem = GetCachedLoginAttempt(true);

            loginCacheItem.CaptchaText = GetRandomWord(_settings.CaptchaWordLength, CaptchaCharacters);
            //model.CaptchaText = loginCacheItem.CaptchaText;

            return(View(model));
        }
        private LoginCacheItem GetCachedLoginAttempt(bool createIfNotExist)
        {
            LoginCacheItem Result = null;

            string cacheId = _settings.CacheUseSession ? GetCoreSessionId() : GetIpAddress();

            CacheItem loginCache = _loginCache.Get(cacheId);

            if (loginCache != null)
            {
                Result = (LoginCacheItem)loginCache.Value;
            }
            else if (createIfNotExist && loginCache == null)
            {
                Result     = new LoginCacheItem();
                loginCache = new CacheItem(cacheId, Result);
                _loginCache.Add(cacheId, loginCache);
            }

            return(Result);
        }
        public IActionResult Index(LoginViewModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            LoginCacheItem loginCacheItem = GetCachedLoginAttempt(true);

            if (!String.IsNullOrEmpty(loginCacheItem.CaptchaText))
            {
                if (!loginCacheItem.CaptchaText.Equals(model.CaptchaText))
                {
                    ModelState.AddModelError(String.Empty, Languages.LanguageStrings.CodeNotValid);
                }
            }

            loginCacheItem.LoginAttempts++;

            model.ShowCaptchaImage = loginCacheItem.LoginAttempts >= _settings.CaptchaShowFailCount;

            UserLoginDetails loginDetails = new UserLoginDetails();

            model.Breadcrumbs = GetBreadcrumbs();
            model.CartSummary = GetCartSummary();

            LoginResult loginResult = _loginProvider.Login(model.Username, model.Password, GetIpAddress(),
                                                           loginCacheItem.LoginAttempts, ref loginDetails);

            switch (loginResult)
            {
            case LoginResult.Success:
            case LoginResult.PasswordChangeRequired:
                RemoveLoginAttempt();

                UserSession session = GetUserSession();

                if (session != null)
                {
                    session.Login(loginDetails.UserId, loginDetails.Username, loginDetails.Email);
                }

                if (model.RememberMe)
                {
                    CookieAdd(_settings.RememberMeCookieName, Encrypt(loginDetails.UserId.ToString(),
                                                                      _settings.EncryptionKey), _settings.LoginDays);
                }


                GetAuthenticationService().SignInAsync(HttpContext,
                                                       _settings.AuthenticationScheme,
                                                       new ClaimsPrincipal(_claimsProvider.GetUserClaims(loginDetails.UserId)),
                                                       _claimsProvider.GetAuthenticationProperties());

                if (loginResult == LoginResult.PasswordChangeRequired)
                {
                    return(Redirect(_settings.ChangePasswordUrl));
                }

                return(Redirect(model.ReturnUrl));

            case LoginResult.AccountLocked:
                return(RedirectToAction(nameof(AccountLocked), new { username = model.Username }));

            case LoginResult.InvalidCredentials:
                ModelState.AddModelError(String.Empty, Languages.LanguageStrings.InvalidUsernameOrPassword);
                break;
            }

            if (model.ShowCaptchaImage)
            {
                loginCacheItem.CaptchaText = GetRandomWord(_settings.CaptchaWordLength, CaptchaCharacters);
            }

            return(View(model));
        }