Ejemplo n.º 1
0
        public AdminView PersistLogin()
        {
            AdminView admin;

            string sessionValue = session.GetString(adminSessionKeyWord);

            if (sessionValue != null)
            {
                admin = eCommerce.GetAdminBy(int.Parse(sessionValue));
                if (admin != null)
                {
                    return(admin);
                }
                session.Remove(adminSessionKeyWord);
                return(null);
            }

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

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

            admin = eCommerce.GetAdminBy(loginCookies.UserId);
            if (admin == null)
            {
                responseCookies.Delete(adminCookieKeyWord);
                return(null);
            }

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

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

            session.SetString(adminSessionKeyWord, admin.Id);
            return(admin);
        }
Ejemplo n.º 2
0
        public IActionResult Login(LoginViewModel loginViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(loginViewModel));
            }
            IList <string> errors = new List <string>();
            AdminView      admin  = loginPersistence.PersistLogin();

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

            if (errors.Any())
            {
                ViewBag.LoginErrors = errors;
                return(View(loginViewModel));
            }
            return(Redirect(loginViewModel.ReturnUrl));
        }