Esempio n. 1
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (ModelState.IsValid)
            {
                ApplicationUser?user = await userManager.FindByEmailAsync(Email);

                if (user is null)
                {
                    ModelState.AddModelError(string.Empty, "Verification email sent. Please check your email.");
                    return(Page());
                }

                // Generates a new confirmation code and encodes it
                var userId = await userManager.GetUserIdAsync(user);

                var code = await userManager.GenerateEmailConfirmationTokenAsync(user);

                code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
                var callbackUrl = Url.Page(
                    "/Account/ConfirmEmail",
                    null,
                    new { userId, code },
                    Request.Scheme);

                // Sends an email to the user with the account confirmation code
                await smtpService.SendAccountConfirmationEmailAsync(callbackUrl, user);

                ModelState.AddModelError(string.Empty, "Verification email sent. Please check your email.");
                return(Page());
            }

            return(Page());
        }
Esempio n. 2
0
        // Submit new user button click
        public async Task <IActionResult> OnPostAsync(string?returnUrl = null)
        {
            returnUrl ??= Url.Content("~/");
            ExternalLogins = (await signInManager.GetExternalAuthenticationSchemesAsync()).ToList();

            if (ModelState.IsValid)
            {
                // Creates the new user and it's identity
                AppUser.UserName = AppUser.Email;
                IdentityResult result = await userManager.CreateAsync(AppUser, AppUser.Password);

                if (result.Succeeded)
                {
                    // Generates the user account confirmation code
                    logger.LogInformation($"User {AppUser.Email} created a new account with password.");
                    var code = await userManager.GenerateEmailConfirmationTokenAsync(AppUser);

                    code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
                    var callbackUrl = Url.Page("/Account/ConfirmEmail", null, new
                    {
                        area   = "Identity",
                        userId = AppUser.Id,
                        code,
                        returnUrl
                    }, Request.Scheme);

                    // Sends a confirmation email to the user
                    await smtpService.SendAccountConfirmationEmailAsync(callbackUrl, AppUser);

                    // Redirect to the account confirmation page
                    return(RedirectToPage("RegisterConfirmation", new { email = AppUser.Email, returnUrl }));
                }

                foreach (IdentityError?error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }