Пример #1
0
        public async Task <IActionResult> OnGetAsync()
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'."));
            }

            var userName = await _userManager.GetUserNameAsync(user);

            var email = await _userManager.GetEmailAsync(user);

            var phoneNumber = await _userManager.GetPhoneNumberAsync(user);

            Username = userName;

            Input = new InputModel
            {
                Email         = email,
                PhoneNumber   = phoneNumber,
                StatusMessage = StatusMessage,
                Forename      = user.Forename,
                Surname       = user.Surname,
                Title         = user.Title,
                Titles        = CustomerTitle.GetTitles(_context.Titles.Where(c => c.Id > 0).OrderBy(x => x.Value).ToList())
            };

            IsEmailConfirmed = await _userManager.IsEmailConfirmedAsync(user);

            return(Page());
        }
 public void OnGet(string returnUrl = null)
 {
     Input = new InputModel
     {
         Titles = CustomerTitle.GetTitles(_context.Titles.Where(c => c.Id > 0).OrderBy(x => x.Value).ToList())
     };
     ReturnUrl = returnUrl;
 }
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl = returnUrl ?? Url.Content("~/");
            if (ModelState.IsValid)
            {
                if (string.IsNullOrWhiteSpace(Input.RecaptchaResponse))
                {
                    Input.FailureMessage = "Something went wrong, please try again.";
                    Input.Titles         = CustomerTitle.GetTitles(_context.Titles.Where(c => c.Id > 0).OrderBy(x => x.Value).ToList());
                    return(Page());
                }

                if (!RecaptchaHelper.IsReCaptchValid(Input.RecaptchaResponse))
                {
                    Input.FailureMessage = "Something went wrong, please try again.";
                    Input.Titles         = CustomerTitle.GetTitles(_context.Titles.Where(c => c.Id > 0).OrderBy(x => x.Value).ToList());
                    return(Page());
                }

                var user = new ApplicationUser {
                    UserName = Input.Email,
                    Email    = Input.Email,
                    Forename = Input.Forename,
                    Surname  = Input.Surname,
                    Title    = Input.Title
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { userId = user.Id, code = code },
                        protocol: Request.Scheme);

                    await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                                                      $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

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

                    return(LocalRedirect(returnUrl));
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            // If we got this far, something failed, redisplay form
            Input.FailureMessage = "Something went wrong, please try again.";
            Input.Titles         = CustomerTitle.GetTitles(_context.Titles.Where(c => c.Id > 0).OrderBy(x => x.Value).ToList());
            return(Page());
        }
        public IActionResult AddStaff(string statusMessage = null, string successMessage = null, string failureMessage = null)
        {
            var viewModel = new InputViewModel
            {
                Titles    = CustomerTitle.GetTitles(_context.Titles.Where(c => c.Id > 0).OrderBy(x => x.Value).ToList()),
                ReturnUrl = "~/ManageStaff/AddStaff"
            };

            return(View(viewModel));
        }
        public async Task <IActionResult> AddStaff(InputViewModel input, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;

            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName = input.Email,
                    Email    = input.Email,
                    Forename = input.Forename,
                    Surname  = input.Surname,
                    Title    = input.Title
                };

                var result = await _userManager.CreateAsync(user, input.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { userId = user.Id, code = code },
                        protocol: Request.Scheme);

                    await _emailSender.SendEmailAsync(input.Email, "Confirm your email",
                                                      $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

                    await _userManager.AddToRoleAsync(user, MasterStrings.StaffRole);

                    return(ControllerHelper.RedirectToLocal(this, "/ManageStaff?successMessage=" + input.Forename + " added."));
                }

                ControllerHelper.AddErrors(this, result);

                input.FailureMessage = "Something went wrong.";
            }

            input.Titles = CustomerTitle.GetTitles(_context.Titles.Where(c => c.Id > 0).OrderBy(x => x.Value).ToList());
            return(View(input));
        }