public CustomerView PersistLogin()
        {
            CustomerView customer;

            //check if session existed
            string sessionValue = session.GetString(customerSessionKeyWord);

            if (sessionValue != null)
            {
                customer = eCommerce.GetCustomerBy(int.Parse(sessionValue));
                if (customer != null)
                {
                    if (customer.Active)
                    {
                        return(customer);
                    }
                }
                session.Remove(customerSessionKeyWord);
                return(null);
            }

            LoginCookies loginCookies = requestCookies.GetJson <LoginCookies>(customerCookieKeyWord);

            if (loginCookies == null)
            {
                return(null);
            }

            customer = eCommerce.GetCustomerBy(loginCookies.UserId);
            if (customer == null)
            {
                responseCookies.Delete(customerCookieKeyWord);
                return(null);
            }

            if (!customer.Active)
            {
                responseCookies.Delete(customerCookieKeyWord);
                return(null);
            }

            string loginValue = EncryptionService.Encrypt(customer.Email +
                                                          eCommerce.GetCustomerEncryptedPassword(int.Parse(customer.Id)) +
                                                          connectionInfo.RemoteIpAddress.ToString());

            if (loginCookies.LoginValue != loginValue)
            {
                responseCookies.Delete(customerCookieKeyWord);
                return(null);
            }

            session.SetString(customerSessionKeyWord, customer.Id);
            return(customer);
        }
        public IActionResult Login(LoginViewModel loginViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(loginViewModel));
            }
            IList <string> errors   = new List <string>();
            CustomerView   customer = loginPersistence.PersistLogin();

            if (customer == null)
            {
                if (EmailValidationService.IsValidEmail(loginViewModel.LoginInformation.Username))
                {
                    customer = eCommerce.GetCustomerBy(loginViewModel.LoginInformation.Username);
                    if (customer != null)
                    {
                        if (customer.Active)
                        {
                            string encryptedPassword = eCommerce.GetCustomerEncryptedPassword(int.Parse(customer.Id));
                            if (EncryptionService.Encrypt(loginViewModel.LoginInformation.Password) == encryptedPassword)
                            {
                                loginPersistence.LoginThrough(loginViewModel.LoginInformation.Username, loginViewModel.LoginInformation.Remember);
                            }
                            else
                            {
                                errors.Add("Wrong password");
                            }
                        }
                        else
                        {
                            errors.Add("Account was locked");
                        }
                    }
                    else
                    {
                        errors.Add("Email not found");
                    }
                }
                else
                {
                    errors.Add("Invalid email address");
                }
            }
            else
            {
                return(Redirect(loginViewModel.ReturnUrl));
            }

            if (errors.Any())
            {
                ViewData[GlobalViewBagKeys.Errors] = errors;
                return(View(loginViewModel));
            }
            return(Redirect(loginViewModel.ReturnUrl));
        }