Exemplo n.º 1
0
        [LoginDisallowed] // You post this when you are logged in
        public ActionResult Login(LoginModel loginModel)
        {
            // Make sure the model is filled
            if (loginModel.Email == null)
            {
                // Email was not filled in
                ViewBag.Error = "E-mail niet ingevuld.";
                return(View());
            }
            else if (loginModel.Password == null)
            {
                // Password was not filled in
                ViewBag.Error = "Wachtwoord niet ingevuld.";
                return(View());
            }

            using (var db = new EntityContext())
            {
                // Generate the password by hashing the given string
                string password = PasswordHelper.ComputeSha256Hash(loginModel.Password);

                // Check if the user can be found in the database
                User user = db.User
                            .Where(x => x.Email == loginModel.Email)
                            .Where(x => x.PasswordHash == password)
                            .FirstOrDefault();

                // Check if the user was even found
                if (user == null)
                {
                    // No user was found with this email or password.
                    ViewBag.Error = "Ongeldige gebruikersnaam of wachtwoord.";
                    return(View());
                }

                string fullName = $"{user.FirstName} {user.LastName}";

                // Create the user identity for the session
                UserIdentity identity = new UserIdentity(
                    fullName,
                    "userAuth",
                    user.UserID,
                    user.Email,
                    user.FirstName,
                    user.LastName,
                    user.Role.RoleName
                    );

                // Create the user principal
                var principal = new GenericPrincipal(
                    identity,
                    new string[] { user.Role.RoleKey }
                    );

                // Set the principal in the session
                Session["principal"] = principal;

                return(RedirectToAction("Index", "YearList"));
            }
        }
Exemplo n.º 2
0
        [LoginDisallowed] // You post this when you are logged in
        public ActionResult Register(RegisterModel registerModel)
        {
            // Make sure the model is filled
            if (registerModel.Email == null)
            {
                // Email was not filled in
                ViewBag.Error = "E-mail niet ingevuld.";
                return(View());
            }
            else if (registerModel.FirstName == null)
            {
                // First name was not filled in
                ViewBag.Error = "Voornaam niet ingevuld.";
                return(View());
            }
            else if (registerModel.LastName == null)
            {
                // Last name was not filled in
                ViewBag.Error = "Achternaam niet ingevuld.";
                return(View());
            }
            else if (registerModel.Password == null)
            {
                // Password was not filled in
                ViewBag.Error = "Wachtwoord niet ingevuld.";
                return(View());
            }

            using (var db = new EntityContext())
            {
                // Check if user with email already exists
                User duplicateUser = db.User
                                     .Where(x => x.Email == registerModel.Email)
                                     .FirstOrDefault();

                // If the user with the mail was already found, I must be a duplicate
                if (duplicateUser != null)
                {
                    ViewBag.Error = "Gebruiker met deze e-mail bestaat al.";
                    return(View());
                }

                // Create the new user
                User registerdUser = new User
                {
                    Email        = registerModel.Email,
                    FirstName    = registerModel.FirstName,
                    LastName     = registerModel.LastName,
                    PasswordHash = PasswordHelper.ComputeSha256Hash(registerModel.Password),
                    RoleID       = 1 // Default role = user
                };

                // Save the new user to the database
                db.User.Add(registerdUser);
                db.SaveChanges();

                ViewBag.Success = "Je hebt succesvol een account aangemaakt!";
                return(View());
            }
        }
Exemplo n.º 3
0
        private void loginButton_Click(object sender, RoutedEventArgs e)
        {
            // Turn pincode text into sha256
            var pincode = passwordHelper.ComputeSha256Hash(pincodeInput.Text);

            if (authenticationHelper.adminAuthentication(emailInput.Text, pincode))
            {
                if (!authenticationHelper.userIsBlocked(emailInput.Text))
                {
                    // Get user information to pass on.
                    var user = authenticationHelper.getUserAdmin(emailInput.Text, pincode);

                    // Login successful! Open overview panel!
                    new Forms.Overview(user).Show();
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Account is geblokkeerd", "Error");
                }
            }
            else
            {
                MessageBox.Show("Ongeldig E-Mail adres of pincode!", "Inlog fout");
            }
        }
Exemplo n.º 4
0
        public IActionResult Register([FromBody] User User)
        {
            if (_userRepository.GetByUserName(User.Username) != null)
            {
                return(StatusCode(409));
            }

            User.Password = PasswordHelper.ComputeSha256Hash(User.Password);

            if (_userRepository.Add(User))
            {
                return(Ok());
            }
            return(StatusCode(500));
        }
Exemplo n.º 5
0
 public IActionResult Login([FromBody] User user)
 {
     user.Password = PasswordHelper.ComputeSha256Hash(user.Password);
     return(Ok(_userRepository.Authenticate(user.Username, user.Password)));
 }
Exemplo n.º 6
0
 public IActionResult SignUp([FromBody] User user)
 {
     user.Password = PasswordHelper.ComputeSha256Hash(user.Password);
     return(Ok(_userRepository.AddUser(user)));
 }
Exemplo n.º 7
0
        public ActionResult Edit(UserEditModel editModel)
        {
            try
            {
                using (var db = new EntityContext())
                {
                    User user = db.User
                                .Where(x => x.UserID == editModel.ID)
                                .FirstOrDefault();

                    // Update default details
                    if (editModel.FirstName != null)
                    {
                        user.FirstName = editModel.FirstName;
                    }
                    if (editModel.LastName != null)
                    {
                        user.LastName = editModel.LastName;
                    }

                    // Special details
                    if (editModel.Email != null && editModel.Email != user.Email)
                    {
                        User duplicateUser = db.User
                                             .Where(x => x.Email == editModel.Email)
                                             .FirstOrDefault();

                        // Make sure the email is not already used
                        if (duplicateUser != null)
                        {
                            // The email is already in use
                            ViewBag.Error = "Gebruiker met deze e-mail bestaat al.";
                            return(View(editModel));
                        }
                        else
                        {
                            user.Email = editModel.Email;
                        }
                    }

                    if (editModel.Password != null)
                    {
                        // If the password should be updated, rehash it and put it in the db
                        user.PasswordHash = PasswordHelper.ComputeSha256Hash(editModel.Password);
                    }

                    // Check if the role should be updated
                    if (editModel.RoleKey != user.Role.RoleKey)
                    {
                        // If the role should be updated, update the role to its role id
                        var role = db.Role.Where(x => x.RoleKey == editModel.RoleKey).FirstOrDefault();

                        // Make sure the role exists so the user wont be assigned to a non existing role
                        if (role == null)
                        {
                            ViewBag.Error = "De rol bestaat niet!";
                            return(View(editModel));
                        }

                        // Update the actual role
                        user.RoleID = role.RoleID;
                    }

                    // Save the updates user to the database
                    db.SaveChanges();

                    return(RedirectToAction("List"));
                }
            }
            catch
            {
                return(View());
            }
        }