Пример #1
0
        public CAdmincredential GetOneAdminCredential(string email)
        {
            using var context = new Project0databaseContext(_contextOptions);
            var dbAdmincredential = context.Admincredentials.FirstOrDefault(x => x.Email == email);

            if (dbAdmincredential == null)
            {
                return(null);
            }
            CAdmincredential a = new CAdmincredential(dbAdmincredential.Email, dbAdmincredential.Password);

            return(a);
        }
Пример #2
0
        public ActionResult Index(LoginViewModel viewLogin)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    ModelState.AddModelError("", "Invalid login format");
                    return(View());
                }

                MailAddress result;
                if (!MailAddress.TryCreate(viewLogin.Email, out result))
                {
                    ModelState.AddModelError("", "Invalid login format");
                    return(View());
                }

                // admin login
                CAdmincredential cAdmin = _storeRepo.GetOneAdminCredential(viewLogin.Email);
                if (cAdmin != null)
                {
                    if (cAdmin.Password == viewLogin.Password)
                    {
                        // admin successful login
                        TempData["User"] = viewLogin.Email;
                        TempData.Keep("User");
                        // each user can store some information
                        TempData[viewLogin.Email] = 1;
                        return(RedirectToAction("Index", "Admin"));
                    }
                }

                // memeber login
                CCredential cCredential = _storeRepo.GetOneCredential(viewLogin.Email);
                if (cCredential == null)
                {
                    ModelState.AddModelError("", "This email address has not been registered");
                    return(View());
                }

                if (cCredential.Password == viewLogin.Password)
                {
                    // user successful login
                    TempData["User"] = viewLogin.Email;
                    TempData.Keep("User");
                    TempData[viewLogin.Email] = 1;
                }
                else
                {
                    ModelState.AddModelError("", "Password does not match");
                    return(View());
                }
                // relative path
                return(RedirectToAction("Index", "Store"));
            }
            catch (Exception e)
            {
                _logger.LogError(e, "error while tring to login");
                ModelState.AddModelError("", "failed to login");
                return(View());
            }
        }