Ejemplo n.º 1
0
        public ActionResult Create(UserCreateViewModel userViewModel)
        {
            if (LoggedInAsAdmin())
            {
                try
                {
                    if (ModelState.IsValid)
                    {
                        ApplicationUser user = _mapper.Map <ApplicationUser>(userViewModel);

                        user.PasswordHash = SHA512PasswordHasher.Hash(userViewModel.PasswordHash);
                        _userService.Create(user);
                        return(RedirectToAction(nameof(Index)));
                    }

                    return(View());
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError(string.Empty, ex.Message);
                    return(View());
                }
            }
            return(RedirectToAction("Login", "User"));
        }
Ejemplo n.º 2
0
        public ActionResult Login(LoginViewModel loginViewModel)
        {
            try
            {
                if (loginViewModel.Username.Equals("dotnetcore", StringComparison.OrdinalIgnoreCase))
                {
                    bool isAdmin = _authenticUserService.CheckConnection(loginViewModel.Username, loginViewModel.Password);
                    if (isAdmin)
                    {
                        HttpContext.Session.Set("Username", loginViewModel.Username);
                        return(RedirectToAction("Index", "Admin"));
                    }
                }

                loginViewModel.Password = SHA512PasswordHasher.Hash(loginViewModel.Password);

                var user = _authenticUserService.GetMyInfo(loginViewModel.Username, loginViewModel.Password);

                HttpContext.Session.Set("UserId", user.Id);
                HttpContext.Session.Set("Username", loginViewModel.Username);

                ProfileViewModel userVM = _mapper.Map <ProfileViewModel>(user);

                return(View("Profile", userVM));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return(View());
            }
        }
Ejemplo n.º 3
0
        public ActionResult Edit(int id, UserEditViewModel userEditViewModel)
        {
            if (LoggedInAsAdmin())
            {
                try
                {
                    if (ModelState.IsValid)
                    {
                        var userFromDb = _userService.FindById(id);

                        if (userFromDb.Username != userEditViewModel.Username)
                        {
                            TempData["postError"] = "Don't try to hack my site, brooo!";
                            return(RedirectToAction(nameof(Edit), id));
                        }

                        _mapper.Map(userEditViewModel, userFromDb);
                        if (!string.IsNullOrEmpty(userEditViewModel.PasswordHash))
                        {
                            userFromDb.PasswordHash = SHA512PasswordHasher.Hash(userEditViewModel.PasswordHash);
                        }


                        _userService.Update(id, userFromDb);

                        return(RedirectToAction(nameof(Index)));
                    }

                    return(Edit(id));
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError(string.Empty, ex.Message);
                    return(View());
                }
            }
            return(RedirectToAction("Login", "User"));
        }