Ejemplo n.º 1
0
        public IActionResult Login(LogRegModel model)
        {
            LoginUser user = model.Login;

            if (ModelState.IsValid)
            {
                // is this user's email in the db???
                DashboardUser userCheck = dbContext.Users.FirstOrDefault(u => u.Email == user.EmailAttempt);
                if (userCheck == null)
                {
                    // we have a problem
                    ModelState.AddModelError("Login.EmailAttempt", "Invalid Email/Password");
                    return(View("Index"));
                }
                else
                {
                    // check for db.password == form.password
                    PasswordHasher <LoginUser> hasher = new PasswordHasher <LoginUser>();
                    PasswordVerificationResult result = hasher.VerifyHashedPassword(user, userCheck.Password, user.PasswordAttempt);
                    if (result == 0)
                    {
                        // we have a problem
                        ModelState.AddModelError("Login.EmailAttempt", "Invalid Email/Password");
                        return(View("Index"));
                    }
                    else
                    {
                        // we made it!
                        HttpContext.Session.SetInt32("userId", userCheck.UserId);
                        return(RedirectToAction("Index", "Dashboard"));
                    }
                }
            }
            return(View("Index"));
        }
Ejemplo n.º 2
0
        public IActionResult Login(LogRegModel model)
        {
            LoginUser user = model.Attempt;

            if (ModelState.IsValid)
            {
                User toLogin = dbContext.Users.FirstOrDefault(u => u.Email == user.EmailAttempt);
                if (toLogin == null)
                {
                    ModelState.AddModelError("Attempt.EmailAttempt", "Invalid Email/Password");
                    return(View("Index"));
                }
                PasswordHasher <LoginUser> hasher = new PasswordHasher <LoginUser>();
                var result = hasher.VerifyHashedPassword(user, toLogin.Password, user.PasswordAttempt);
                if (result == PasswordVerificationResult.Failed)
                {
                    ModelState.AddModelError("Attempt.EmailAttempt", "Invalid Email/Password");
                    return(View("Index"));
                }
                // Log user into session
                HttpContext.Session.SetInt32("userId", toLogin.UserId);
                return(RedirectToAction("Index", "Post"));
            }
            return(View("Index"));
        }
Ejemplo n.º 3
0
        public IActionResult Create(LogRegModel model)
        {
            DashboardUser user = model.Register;

            if (ModelState.IsValid)
            {
                // QUERY FOR UNIQUE EMAIL
                if (dbContext.Users.Any(u => u.Email == user.Email))
                {
                    // we have a problem
                    ModelState.AddModelError("Register.Email", "Email already in use!");
                    return(View("Index"));
                }

                // if all checks out, put them in the db!
                PasswordHasher <DashboardUser> hasher = new PasswordHasher <DashboardUser>();
                string hashedPw = hasher.HashPassword(user, user.Password);
                user.Password = hashedPw;

                dbContext.Users.Add(user);
                dbContext.SaveChanges();

                return(RedirectToAction("Index", "Dashboard"));
            }

            return(View("Index"));
        }
Ejemplo n.º 4
0
 public IActionResult Login(LogRegModel model)
 {
     if (ModelState.IsValid)
     {
         var checkemail = _wcontext.users.SingleOrDefault(u => u.email == model.loguser.email);
         if (checkemail != null)
         {
             PasswordHasher <LogUser> hasher = new PasswordHasher <LogUser>();
             if (hasher.VerifyHashedPassword(model.loguser, checkemail.password, model.loguser.password) != PasswordVerificationResult.Failed)
             {
                 HttpContext.Session.SetInt32("loggin_id", checkemail.user_id);
                 return(RedirectToAction("Dashboard", "Dashboard", new { id = checkemail.user_id }));
             }
             else
             {
                 ModelState.AddModelError("password", "Wrong Password !");
             }
         }
         else
         {
             ModelState.AddModelError("email", "Email does not exist!");
         }
     }
     return(View("Home", model));
 }
Ejemplo n.º 5
0
        public IActionResult Create(LogRegModel model)
        {
            User user = model.NewUser;

            if (ModelState.IsValid)
            {
                if (dbContext.Users.Any(u => u.Email == user.Email))
                {
                    ModelState.AddModelError("NewUser.Email", "Email already in use");
                    return(View("Index"));
                }

                PasswordHasher <User> hasher = new PasswordHasher <User>();
                user.Password = hasher.HashPassword(user, user.Password);

                var newUser = dbContext.Users.Add(user).Entity;
                dbContext.SaveChanges();

                HttpContext.Session.SetInt32("userId", newUser.UserId);

                return(RedirectToAction("Index", "Post"));
            }

            return(View("Index"));
        }
        public IActionResult Login(LogRegModel model)
        {
            LogUser user = model.LogUser;

            if (ModelState.IsValid)
            {
                //check to see if user exists in db
                User check_user_exists = dbContext.Users.FirstOrDefault(u => u.Email == user.LogEmail);

                //If user not in db
                if (check_user_exists == null)
                {
                    ModelState.AddModelError("LogUser.LogEmail", "Invalid Email address or Password");
                    return(View("Index"));
                }

                //Compare hashed password to confirmation password
                PasswordHasher <LogUser> checkHash = new PasswordHasher <LogUser>();
                var verified_user = checkHash.VerifyHashedPassword(user, check_user_exists.Password, user.LogPassword);

                //If user cannot be verified
                if (verified_user == 0)
                {
                    ModelState.AddModelError("LogUser.LogEmail", "Invalid Email/Password");
                    return(View("Index"));
                }

                //Place logged in user's id in session.
                HttpContext.Session.SetInt32("id", check_user_exists.UserId);

                return(RedirectToAction("Success"));
            }
            return(View("Index"));
        }
        public IActionResult Register(LogRegModel model)
        {
            User newUser = model.RegUser;

            //Input fields correct
            if (ModelState.IsValid)
            {
                bool notUnique = dbContext.Users.Any(a => a.Email == newUser.Email);

                if (notUnique)
                {
                    ModelState.AddModelError("Email", "Email already exists, please try a new one");
                    return(View("Index"));
                }

                // Hash user's password
                PasswordHasher <User> hasher = new PasswordHasher <User>();
                string hash = hasher.HashPassword(newUser, newUser.Password);
                newUser.Password = hash;

                //Add user to database
                dbContext.Users.Add(newUser);
                dbContext.SaveChanges();
                return(RedirectToAction("Success"));
            }
            //Form not valid, redirect to Index page to show errors.
            return(View("Index"));
        }
Ejemplo n.º 8
0
        public async Task <IActionResult> Login(LogRegModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                // This doesn't count login failures towards account lockout
                // To enable password failures to trigger account lockout, set lockoutOnFailure: true
                var result = await _signInManager.PasswordSignInAsync(model.Login.Email, model.Login.Password, model.Login.RememberMe, lockoutOnFailure : false);

                if (result.Succeeded)
                {
                    _logger.LogInformation(1, "User logged in.");
                    return(RedirectToLocal(returnUrl));
                }
                if (result.RequiresTwoFactor)
                {
                    return(RedirectToAction(nameof(SendCode), new { ReturnUrl = returnUrl, RememberMe = model.Login.RememberMe }));
                }
                if (result.IsLockedOut)
                {
                    _logger.LogWarning(2, "User account locked out.");
                    return(View("Lockout"));
                }
                else
                {
                    ModelState.AddModelError("invalid_login", "Invalid login attempt.");
                    return(View("LoginOrRegister", model));
                }
            }

            // If we got this far, something failed, redisplay form
            return(View("LoginOrRegister", model));
        }
        public IActionResult Login(LogRegModel model)
        {
            LogUser user = model.loginUser;

            Dictionary <string, object> userToLog = new Dictionary <string, object>();

            if (ModelState.IsValid)
            {
                string checkEmail = $"SELECT id, first_name, password FROM users WHERE email = '{user.log_email}'";
                userToLog = DbConnector.Query(checkEmail).FirstOrDefault();

                if (userToLog == null)
                {
                    ModelState.AddModelError("log_email", "Invalid Email/Password!");
                }
                else
                {
                    // check hashed password for user with email
                    string hashedPW = (string)userToLog["password"];
                    PasswordHasher <LogUser> hasher = new PasswordHasher <LogUser>();
                    // if VerifyHashedPassword evaluates to 0, we have a fail!
                    if (hasher.VerifyHashedPassword(user, hashedPW, user.log_password) ==
                        PasswordVerificationResult.Failed)
                    {
                        ModelState.AddModelError("log_email", "Invalid Email/Password!");
                    }
                }
            }

            if (ModelState.IsValid)
            {
                // log user in!
                HttpContext.Session.SetInt32("id", (int)userToLog["id"]);
                HttpContext.Session.SetString("name", (string)userToLog["first_name"]);
                // ViewBag.userName = (string)userToLog["first_name"];

                return(RedirectToAction("Index", "Dashboard"));
            }


            return(View("Index", model));
        }
Ejemplo n.º 10
0
        public async Task <IActionResult> Register(LogRegModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                if (!_userManager.Users.Any(x => x.ProfileName == model.Register.ProfileName))
                {
                    var user = new ApplicationUser {
                        UserName = model.Register.Email, Email = model.Register.Email
                    };

                    // ApplicationUser does not take ProfileName in a construtor.
                    user.ProfileName    = model.Register.ProfileName;
                    user.ProfilePicture = Gravatar.GetLink(model.Register.Email);

                    // This calls our overridden function in ProfileNameClaimsPrincipalFactory.cs
                    var result = await _userManager.CreateAsync(user, model.Register.Password);

                    if (result.Succeeded)
                    {
                        // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=532713
                        // Send an email with this link
                        //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                        //var callbackUrl = Url.Action(nameof(ConfirmEmail), "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
                        //await _emailSender.SendEmailAsync(model.Email, "Confirm your account",
                        //    $"Please confirm your account by clicking this link: <a href='{callbackUrl}'>link</a>");

                        await _signInManager.SignInAsync(user, isPersistent : false);

                        _logger.LogInformation(3, "User created a new account with password.");
                        return(RedirectToLocal(returnUrl));
                    }
                    AddErrors(result);
                }
                AddErrors(IdentityResult.Failed(new IdentityError {
                    Description = "Profile Name already taken"
                }));
            }

            // If we got this far, something failed, redisplay form
            return(View("LoginOrRegister", model));
        }
Ejemplo n.º 11
0
        public IActionResult Register(LogRegModel model)
        {
            PasswordHasher <RegUser> hasher = new PasswordHasher <RegUser>();

            if (ModelState.IsValid)
            {
                model.newuser.password = hasher.HashPassword(model.newuser, model.newuser.password);
                Person user = new Person()
                {
                    first_name = model.newuser.first_name,
                    last_name  = model.newuser.last_name,
                    email      = model.newuser.email,
                    password   = model.newuser.password
                };
                _wcontext.users.Add(user);
                _wcontext.SaveChanges();
                TempData["reg"] = "Register successfully, please login !";
                return(RedirectToAction("Home"));
            }

            return(View("Home", model));
        }
        public IActionResult Create(LogRegModel model)
        {
            User user = model.newUser;

            if (ModelState.IsValid)
            {
                if (user.first_name == user.last_name)
                {
                    ModelState.AddModelError("last_name", "The last name cannot be the same as the first name.");
                }
            }
            if (ModelState.IsValid)
            {
                string check_email = $"SELECT id FROM users WHERE email = '{user.email}'";
                if (DbConnector.Query(check_email).Count > 0)
                {
                    ModelState.AddModelError("email", "Email already in use");
                }
            }

            if (ModelState.IsValid)
            {
                // Hash user's password for DB storage
                PasswordHasher <User> hasher = new PasswordHasher <User>();
                string hashedPW = hasher.HashPassword(user, user.password);

                string createUser = $@"INSERT INTO users (first_name, last_name, email, password,
                created_at, updated_at) 
                VALUES ('{user.first_name}', '{user.last_name}', '{user.email}', '{hashedPW}', 
                NOW(), NOW());";
                DbConnector.Execute(createUser);

                TempData["success"] = "You have successfully registered, you may now log in";
            }
            // return RedirectToAction("Index");
            return(View("Index", model));
        }
Ejemplo n.º 13
0
 public IActionResult Login(LogRegModel model)
 {
     LoginUser person = model.Login;
     if (ModelState.IsValid)
     {
         Person toLogin = dbContext.People.FirstOrDefault(u => u.Email == person.EmailAttempt);
         if (toLogin == null)
         {
             ModelState.AddModelError("Login.EmailAttempt", "Invalid Email/Password");
             return View("Index");
         }
         PasswordHasher<LoginUser> hasher = new PasswordHasher<LoginUser>();
         var result = hasher.VerifyHashedPassword(person, toLogin.Password, person.PasswordAttempt);
         if (result == PasswordVerificationResult.Failed)
         {
             ModelState.AddModelError("Login.EmailAttempt", "Invalid Email/Password");
             return View("Index");
         }
         // Log user into session
         HttpContext.Session.SetInt32("userId", toLogin.PersonId);
         return RedirectToAction("Index", "People");
     }
     return View("Index");
 }
Ejemplo n.º 14
0
        public IActionResult Create(LogRegModel model)
        {
            Person person = model.Register;

            if (ModelState.IsValid)
            {
                if (dbContext.People.Any(o => o.Email == person.Email))
                {
                    ModelState.AddModelError("Email", "Email already in use");
                    return View("Index");
                }

                PasswordHasher<Person> hasher = new PasswordHasher<Person>();
                person.Password = hasher.HashPassword(person, person.Password);

                var newPerson = dbContext.People.Add(person).Entity;
                dbContext.SaveChanges();

                HttpContext.Session.SetInt32("userId", newPerson.PersonId);

                return RedirectToAction("Index", "People");
            }
            return View("Index");
        }