Beispiel #1
0
        public IActionResult Login(LoginRegWrapper FromForm)
        {
            if (ModelState.IsValid)
            {
                User ExistsInDb = DbContext.Users.FirstOrDefault(u => u.Email == FromForm.Login.Email);

                if (ExistsInDb == null)
                {
                    ModelState.AddModelError("Login.Emial", "Invalid Email/Password");
                    return(LoginReg());
                }
                PasswordHasher <LoginUser> Hasher = new PasswordHasher <LoginUser>();
                PasswordVerificationResult Result = Hasher.VerifyHashedPassword(FromForm.Login, ExistsInDb.Password, FromForm.Login.Password);

                if (Result == 0)
                {
                    ModelState.AddModelError("Login.Email", "Invalid Email/Password");
                    return(LoginReg());
                }
                HttpContext.Session.SetInt32("UserId", ExistsInDb.UserId);
                return(RedirectToAction("Homepage"));
            }
            else
            {
                return(LoginReg());
            }
        }
Beispiel #2
0
        public IActionResult Register(LoginRegWrapper FromForm)
        {
            if (ModelState.IsValid)
            {
                if (DbContext.Users.Any(u => u.Email == FromForm.Register.Email))
                {
                    ModelState.AddModelError("Register.Email", "This Email exists already. Please Log in.");
                    return(LoginReg());
                }
                PasswordHasher <User> Hasher = new PasswordHasher <User>();
                FromForm.Register.Password = Hasher.HashPassword(FromForm.Register, FromForm.Register.Password);

                DbContext.Add(FromForm.Register);
                DbContext.SaveChanges();

                HttpContext.Session.SetInt32("UserId", FromForm.Register.UserId);
                return(RedirectToAction("HomePage"));
            }
            else
            {
                return(LoginReg());
            }
        }