public async Task <IActionResult> OnPostAsync()
        {
            if (ModelState.IsValid)
            {
                var user = await _userManager.FindByEmailAsync(Input.Email);

                if (user == null || !(await _userManager.IsEmailConfirmedAsync(user)))
                {
                    // Don't reveal that the user does not exist or is not confirmed
                    return(RedirectToPage("./ForgotPasswordConfirmation"));
                }

                // For more information on how to enable account confirmation and password reset please
                // visit https://go.microsoft.com/fwlink/?LinkID=532713
                var code = await _userManager.GeneratePasswordResetTokenAsync(user);

                code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
                var callbackUrl = Url.Page(
                    "/Account/ResetPassword",
                    pageHandler: null,
                    values: new { area = "Identity", code },
                    protocol: Request.Scheme);

                await AmazonEmailService.SendEmail(
                    _configuration.GetValue <string>("EmailSender"),
                    Input.Email,
                    "Reset Password - Simple Project Management",
                    $"Please reset your password by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.",
                    $"Please reset your password by clicking, or copying and pasting this URL into your browser: {HtmlEncoder.Default.Encode(callbackUrl)}.");

                return(RedirectToPage("./ForgotPasswordConfirmation"));
            }

            return(Page());
        }
예제 #2
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl      = returnUrl ?? Url.Content("~/");
            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
            if (ModelState.IsValid)
            {
                var user = new User {
                    UserName = Input.Email, Email = Input.Email
                };
                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);

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

                    await AmazonEmailService.SendEmail(
                        _configuration.GetValue <string>("EmailSender"),
                        Input.Email,
                        "Confirm Email - Simple Project Management",
                        $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.",
                        $"Please confirm your account by clicking, or copying and pasting this URL into your browser: {HtmlEncoder.Default.Encode(callbackUrl)}.");

                    // Add to administrator role if this is the first user
                    if (_context.Users.Count() == 1)
                    {
                        // Get user
                        var firstUser = _context.Users.First();

                        // Add user to administrator role
                        await _userManager.AddToRoleAsync(firstUser, "Administrator");
                    }

                    if (_userManager.Options.SignIn.RequireConfirmedAccount)
                    {
                        return(RedirectToAction("Index", "Home").WithSuccess("Success", "Please check your email to confirm your account"));
                    }
                    else
                    {
                        await _signInManager.SignInAsync(user, isPersistent : false);

                        return(Redirect("/user"));
                    }
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

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