Esempio n. 1
0
        public IActionResult Login(User user)
        {
            ModelState.Clear();
            ViewBag.login = true;
            var allUsers = _userDataAccess.GetAllUsers();
            var userId   = _userDataAccess.GetUserId(user);

            user.Id = userId;
            var userInfo = _userDataAccess.GetUserById(user.Id);

            if (!allUsers.Any(x => x.Email == user.Email))
            {
                ModelState.AddModelError(string.Empty, "User with this email doesn't exists!");
            }
            else if (!userInfo.ConfirmAccount)
            {
                ModelState.AddModelError(string.Empty, "We have send an email to your email account, please confirm it!");
            }
            else if (ModelState.IsValid)
            {
                var userEnteredPass = HashPassword.ComparePasswordAndHash(user.Password, userInfo.Salt, userInfo.Password);

                if (!userEnteredPass)
                {
                    ModelState.AddModelError("WrongPassword", "Please try again!");
                }
                else
                {
                    try
                    {
                        _userDataAccess.SignIn(HttpContext, user.Id);
                        return(RedirectToAction("Dashboard", "Studio"));
                    }
                    catch (Exception ex)
                    {
                        ModelState.AddModelError("summary", ex.Message);
                        return(View("Views/Account/RegisterLogin.cshtml"));
                    }
                }
            }
            return(View("Views/Account/RegisterLogin.cshtml"));
        }
Esempio n. 2
0
        public IActionResult Register(User user)
        {
            ModelState.Clear();
            if (ModelState.IsValid)
            {
                ViewBag.register = true;
                Studio newStudio = new Studio();

                string saltBytes = HashPassword.GenerateSalt();
                user.Password = HashPassword.HashPasswordFunction(user.Password, saltBytes);
                user.Salt     = saltBytes;

                newStudio.Name = user.StudioName;

                var allUsers   = _userDataAccess.GetAllUsers();
                var allStudios = _studioDataAccess.GetAllStudios();

                if (allUsers.Any(x => x.Username == user.Username))
                {
                    ModelState.AddModelError(string.Empty, "User with this username already exists");
                }

                if (allUsers.Any(x => x.Email == user.Email))
                {
                    ModelState.AddModelError(string.Empty, "User with this email already exists");
                }

                if (allStudios.Any(x => x.Name == user.StudioName))
                {
                    ModelState.AddModelError(string.Empty, "This studio name already exists");
                }


                else if (ModelState.IsValid)
                {
                    try
                    {
                        using (TransactionScope scope = new TransactionScope())
                        {
                            _studioDataAccess.AddNewStudio(newStudio);

                            int studioId = _studioDataAccess.GetStudioId(newStudio);

                            _userDataAccess.AddNewUser(user, studioId);

                            int userId = _userDataAccess.GetUserId(user);

                            var    path    = Url.Action("AuthenticateLogin", "Home", new { userId = user.Id }, protocol: HttpContext.Request.Scheme);
                            string message = "Please confirm your account by clicking <a href=\"" + path + "\">here</a>";

                            _email.SendGrid(user.Email, message);

                            scope.Complete();

                            return(View("Views/Account/ConfirmEmail.cshtml"));
                        }
                    }
                    catch (ThreadAbortException ex)
                    {
                        ModelState.AddModelError(ex.Message, "");
                    }
                }
            }

            return(View("Views/Account/RegisterLogin.cshtml"));
        }