public async Task <IActionResult> Login(LoginViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;

            try
            {
                if (ModelState.IsValid)
                {
                    var result = await _accountService.SignIn(model.Email, model.Password, model.RememberMe);

                    if (result == SignInResult.Success)
                    {
                        return(RedirectToLocal(returnUrl));
                    }
                }
            }
            catch (Exception ex)
            {
                var exceptionMessage = await Helpers.GetErrors(ex, _emailService);

                ModelState.AddModelError(new ValidationResult(exceptionMessage));
            }

            ModelState.AddModelError(string.Empty, "Invalid username or password entered.");
            return(View(model));
        }
        public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;

            try
            {
                if (ModelState.IsValid)
                {
                    var user = new AppUser {
                        UserName = model.Email, Email = model.Email
                    };
                    var result = await _accountService.Register(user, model.Password);

                    if (result.Succeeded)
                    {
                        return(RedirectToLocal(returnUrl));
                    }

                    ModelState.AddIdentityErrors(result);
                }
            }
            catch (Exception ex)
            {
                var exceptionMessage = await Helpers.GetErrors(ex, _emailService);

                ModelState.AddModelError(new ValidationResult(exceptionMessage));
            }

            ModelState.AddModelError(string.Empty, "Invalid register attempt.");
            return(View(model));
        }
        public async Task <IActionResult> ResetPassword(ResetPasswordViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var user = await _accountService.FindUser(model.Email);

                    if (user == null)
                    {
                        // Don't reveal that the user does not exist
                        return(RedirectToAction(nameof(ResetPasswordConfirmation)));
                    }

                    var result = await _accountService.ResetPassword(user, model.Code, model.Password);

                    if (result.Succeeded)
                    {
                        return(RedirectToAction(nameof(ResetPasswordConfirmation)));
                    }
                }
            }
            catch (Exception ex)
            {
                var exceptionMessage = await Helpers.GetErrors(ex, _emailService);

                ModelState.AddModelError(new ValidationResult(exceptionMessage));
            }

            ModelState.AddModelError(string.Empty, "Invalid reset password attempt.");
            return(View(model));
        }
        public async Task <IActionResult> UserRegion(int?id)
        {
            try
            {
                if (User != null)
                {
                    await _accountService.SetUserRegion(User, id);

                    TempData[Constants.Common.ModalMessage] = Constants.Message.RecordSuccessUpdate;
                    return(RedirectToAction(nameof(UserRegion)));
                }

                ModelState.AddModelError(string.Empty, "User not found");

                await _accountService.SignOut();

                return(RedirectToAction(nameof(UserRegion)));
            }
            catch (Exception ex)
            {
                var exceptionMessage = await Helpers.GetErrors(ex, _emailService);

                ModelState.AddModelError(new ValidationResult(exceptionMessage));
            }

            ModelState.AddModelError(string.Empty, "Invalid user update.");

            TempData[Constants.Common.ModalTitle]   = Constants.Message.Error;
            TempData[Constants.Common.ModalMessage] = Helpers.CreateValidationSummary(ModelState);

            return(RedirectToAction(nameof(UserRegion)));
        }
示例#5
0
        public async Task <IActionResult> Delete(int id, RegionSearchViewModel searchViewModel)
        {
            try
            {
                var validationResult = await new RegionHandler(_regionService).CanDelete(id);
                if (validationResult == null)
                {
                    // if no validation error, delete the record
                    await _regionService.DeleteById(id);

                    // set TempData for Success Modal
                    TempData[Constants.Common.ModalMessage] = Constants.Message.RecordSuccessDelete;

                    return(RedirectToAction(nameof(List), searchViewModel));
                }

                ModelState.AddModelError(validationResult);
            }
            catch (Exception ex)
            {
                var exceptionMessage = await Helpers.GetErrors(ex, _emailService);

                ModelState.AddModelError(new ValidationResult(exceptionMessage));
            }

            ModelState.AddModelError(string.Empty, "Invalid delete attempt.");

            // set TempData for Error Modal
            TempData[Constants.Common.ModalTitle]   = Constants.Message.Error;
            TempData[Constants.Common.ModalMessage] = Helpers.CreateValidationSummary(ModelState);

            return(RedirectToAction(nameof(List), searchViewModel));
        }
        public async Task <IActionResult> ForgotPassword(ForgotPasswordViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var user = await _accountService.FindUser(model.Email);

                    if (user == null)
                    {
                        return(RedirectToAction(nameof(ForgotPasswordConfirmation)));
                    }

                    var code = await _accountService.GeneratePasswordResetToken(user);

                    var callbackUrl = Url.ResetPasswordCallbackLink(user.Id, code, Request.Scheme);

                    if (await _emailService.SendForgotPasswordEmail(user, callbackUrl))
                    {
                        return(RedirectToAction(nameof(ForgotPasswordConfirmation)));
                    }
                }
            }
            catch (Exception ex)
            {
                var exceptionMessage = await Helpers.GetErrors(ex, _emailService);

                ModelState.AddModelError(new ValidationResult(exceptionMessage));
            }

            ModelState.AddModelError(string.Empty, "Invalid forgot password attempt.");
            return(View(model));
        }
示例#7
0
        public async Task <IActionResult> Edit(RegionViewModel regionViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(regionViewModel));
            }

            if (regionViewModel.RegionId != null)
            {
                var regionToUpdate = await _regionService.FindById(regionViewModel.RegionId.Value);

                if (regionToUpdate == null)
                {
                    // set IsNew to true and update model on view
                    regionViewModel.IsNew = true;
                    return(HandleDeletedRegion(regionViewModel));
                }

                try
                {
                    var region = _mapper.Map <Region>(regionViewModel);

                    var validationResult = await new RegionHandler(_regionService).CanUpdate(region);
                    if (validationResult == null)
                    {
                        var result = await _regionService.Update(region);

                        if (result.ValidationResults.Count == 0)
                        {
                            TempData[Constants.Common.ModalMessage] = Constants.Message.RecordSuccessUpdate;

                            return(RedirectToAction(nameof(List)));
                        }

                        ModelState.AddModelErrors(result.ValidationResults);
                        // Must clear the model error for the next postback.
                        ModelState.Remove(Constants.Region.RowVersion);

                        // update Row Version on model
                        regionViewModel.RowVersion = result.RowVersion;
                        return(View(regionViewModel));
                    }

                    ModelState.AddModelError(validationResult);

                    return(View(regionViewModel));
                }
                catch (Exception ex)
                {
                    var exceptionMessage = await Helpers.GetErrors(ex, _emailService);

                    ModelState.AddModelError(new ValidationResult(exceptionMessage));
                }
            }
            ModelState.AddModelError(string.Empty, "Invalid update attempt.");

            return(View(regionViewModel));
        }
示例#8
0
        public async Task <IActionResult> Create(RegionViewModel regionViewModel)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(View(regionViewModel));
                }

                // Check if isNew is true, this means that the data is from Edit screen
                // which is deleted and would like to create it again
                if (regionViewModel.IsNew)
                {
                    // clear RegionId and RowVersion since it was deleted
                    regionViewModel.RegionId   = null;
                    regionViewModel.RowVersion = null;
                }

                // Map View model to model for creation
                var region = _mapper.Map <Region>(regionViewModel);

                var validationResult = await new RegionHandler(_regionService).CanAdd(region);
                if (validationResult == null)
                {
                    // if no validation error, create region data
                    await _regionService.Create(region);

                    //set TempData for Success Modal
                    TempData[Constants.Common.ModalMessage] = Constants.Message.RecordSuccessAdd;

                    return(RedirectToAction(nameof(List)));
                }

                ModelState.AddModelError(validationResult);

                return(View(regionViewModel));
            }
            catch (Exception ex)
            {
                var exceptionMessage = await Helpers.GetErrors(ex, _emailService);

                ModelState.AddModelError(new ValidationResult(exceptionMessage));
            }

            ModelState.AddModelError(string.Empty, "Invalid creation attempt.");

            return(View(regionViewModel));
        }
示例#9
0
        public async Task <IActionResult> List(RegionSearchViewModel searchViewModel)
        {
            try
            {
                var list = await _regionService.FindRegions(searchViewModel);

                var tuple = new Tuple <RegionSearchViewModel, PaginatedList <Region> >(searchViewModel, list);

                return(View(tuple));
            }
            catch (Exception ex)
            {
                var exceptionMessage = await Helpers.GetErrors(ex, _emailService);

                ModelState.AddModelError(new ValidationResult(exceptionMessage));
            }

            return(View());
        }
示例#10
0
        public async Task <IActionResult> Create(RegionViewModel regionViewModel)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(View(regionViewModel));
                }

                if (regionViewModel.IsNew)
                {
                    regionViewModel.RegionId   = null;
                    regionViewModel.RowVersion = null;
                }

                var region = _mapper.Map <Region>(regionViewModel);

                var validationResult = await new RegionHandler(_regionService).CanAdd(region);
                if (validationResult == null)
                {
                    await _regionService.Create(region);

                    TempData[Constants.Common.ModalMessage] = Constants.Message.RecordSuccessAdd;

                    return(RedirectToAction(nameof(List)));
                }

                ModelState.AddModelError(validationResult);

                return(View(regionViewModel));
            }
            catch (Exception ex)
            {
                var exceptionMessage = await Helpers.GetErrors(ex, _emailService);

                ModelState.AddModelError(new ValidationResult(exceptionMessage));
            }

            ModelState.AddModelError(string.Empty, "Invalid creation attempt.");

            return(View(regionViewModel));
        }
示例#11
0
        public async Task <IActionResult> Register(RegisterViewModel registerViewModel, string returnUrl = null)
        {
            // Set the return url
            ViewData["ReturnUrl"] = returnUrl;

            try
            {
                if (ModelState.IsValid)
                {
                    var user = new AppUser
                    {
                        UserName  = registerViewModel.Email,
                        Email     = registerViewModel.Email,
                        FirstName = registerViewModel.FirstName,
                        LastName  = registerViewModel.LastName
                    };

                    var result = await _accountService.Register(user, registerViewModel.Password);

                    if (result.Succeeded)
                    {
                        // if successful, this will automatically logged in the user.
                        // Redirect to the return url
                        return(RedirectToLocal(returnUrl));
                    }

                    ModelState.AddIdentityErrors(result);
                }
            }
            catch (Exception ex)
            {
                var exceptionMessage = await Helpers.GetErrors(ex, _emailService);

                ModelState.AddModelError(new ValidationResult(exceptionMessage));
            }

            ModelState.AddModelError(string.Empty, "Invalid register attempt.");
            return(View(registerViewModel));
        }
示例#12
0
        public async Task <IActionResult> ForgotPassword(ForgotPasswordViewModel forgotPassViewModel)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    // Check if user exists
                    var user = await _accountService.FindUserByEmail(forgotPassViewModel.Email);

                    if (user == null)
                    {
                        // Don't reveal that the user does not exist. Redirect to success screen
                        return(RedirectToAction(nameof(ForgotPasswordConfirmation)));
                    }

                    // Generate callback url for forgot password email
                    var code = await _accountService.GeneratePasswordResetToken(user);

                    var callbackUrl = Url.ResetPasswordCallbackLink(user.Id, code, Request.Scheme);

                    if (await _emailService.SendForgotPasswordEmail(user, callbackUrl))
                    {
                        // if success in sending email, redirect to success screen
                        return(RedirectToAction(nameof(ForgotPasswordConfirmation)));
                    }
                }
            }
            catch (Exception ex)
            {
                var exceptionMessage = await Helpers.GetErrors(ex, _emailService);

                ModelState.AddModelError(new ValidationResult(exceptionMessage));
            }

            ModelState.AddModelError(string.Empty, "Invalid forgot password attempt.");
            return(View(forgotPassViewModel));
        }
示例#13
0
        public async Task <IActionResult> Edit(RegionViewModel regionViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(regionViewModel));
            }

            if (regionViewModel.RegionId != null)
            {
                // get region to be updated, if null this means it was deleted by another user
                var regionToUpdate = await _regionService.FindById(regionViewModel.RegionId.Value);

                if (regionToUpdate == null)
                {
                    // set IsNew to true
                    regionViewModel.IsNew = true;
                    // show error and update model to view
                    return(HandleDeletedRegion(regionViewModel));
                }

                try
                {
                    // Map View model to model
                    var region = _mapper.Map <Region>(regionViewModel);

                    var validationResult = await new RegionHandler(_regionService).CanUpdate(region);
                    if (validationResult == null)
                    {
                        // if no error, update region data
                        var result = await _regionService.Update(region);

                        if (result.ValidationResults.Count == 0)
                        {
                            // if no validation errors upon updating, set TempData for Success Modal
                            TempData[Constants.Common.ModalMessage] = Constants.Message.RecordSuccessUpdate;

                            return(RedirectToAction(nameof(List)));
                        }

                        // add all errors to ModelState
                        ModelState.AddModelErrors(result.ValidationResults);
                        // Must clear the model error for the next postback.
                        ModelState.Remove(Constants.Region.RowVersion);
                        // update Row Version on model for next postback
                        regionViewModel.RowVersion = result.RowVersion;
                        return(View(regionViewModel));
                    }

                    ModelState.AddModelError(validationResult);

                    return(View(regionViewModel));
                }
                catch (Exception ex)
                {
                    var exceptionMessage = await Helpers.GetErrors(ex, _emailService);

                    ModelState.AddModelError(new ValidationResult(exceptionMessage));
                }
            }
            ModelState.AddModelError(string.Empty, "Invalid update attempt.");

            return(View(regionViewModel));
        }