source: PostgreSqlDotnetCore/Areas/Identity/Pages/Account/ForgotPassword.cshtml.cs

main
Last change on this file was 2aea0fd, checked in by ElenaMoskova <elena.moskova99@…>, 4 months ago

init commit Elena

  • Property mode set to 100644
File size: 3.3 KB
Line 
1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3#nullable disable
4
5using System;
6using System.ComponentModel.DataAnnotations;
7using System.Text;
8using System.Text.Encodings.Web;
9using System.Threading.Tasks;
10using Microsoft.AspNetCore.Authorization;
11using Microsoft.AspNetCore.Identity;
12using Microsoft.AspNetCore.Identity.UI.Services;
13using Microsoft.AspNetCore.Mvc;
14using Microsoft.AspNetCore.Mvc.RazorPages;
15using Microsoft.AspNetCore.WebUtilities;
16
17namespace PostgreSqlDotnetCore.Areas.Identity.Pages.Account
18{
19 public class ForgotPasswordModel : PageModel
20 {
21 private readonly UserManager<IdentityUser> _userManager;
22 private readonly IEmailSender _emailSender;
23
24 public ForgotPasswordModel(UserManager<IdentityUser> userManager, IEmailSender emailSender)
25 {
26 _userManager = userManager;
27 _emailSender = emailSender;
28 }
29
30 /// <summary>
31 /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
32 /// directly from your code. This API may change or be removed in future releases.
33 /// </summary>
34 [BindProperty]
35 public InputModel Input { get; set; }
36
37 /// <summary>
38 /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
39 /// directly from your code. This API may change or be removed in future releases.
40 /// </summary>
41 public class InputModel
42 {
43 /// <summary>
44 /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
45 /// directly from your code. This API may change or be removed in future releases.
46 /// </summary>
47 [Required]
48 [EmailAddress]
49 public string Email { get; set; }
50 }
51
52 public async Task<IActionResult> OnPostAsync()
53 {
54 if (ModelState.IsValid)
55 {
56 var user = await _userManager.FindByEmailAsync(Input.Email);
57 if (user == null || !(await _userManager.IsEmailConfirmedAsync(user)))
58 {
59 // Don't reveal that the user does not exist or is not confirmed
60 return RedirectToPage("./ForgotPasswordConfirmation");
61 }
62
63 // For more information on how to enable account confirmation and password reset please
64 // visit https://go.microsoft.com/fwlink/?LinkID=532713
65 var code = await _userManager.GeneratePasswordResetTokenAsync(user);
66 code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
67 var callbackUrl = Url.Page(
68 "/Account/ResetPassword",
69 pageHandler: null,
70 values: new { area = "Identity", code },
71 protocol: Request.Scheme);
72
73 await _emailSender.SendEmailAsync(
74 Input.Email,
75 "Reset Password",
76 $"Please reset your password by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");
77
78 return RedirectToPage("./ForgotPasswordConfirmation");
79 }
80
81 return Page();
82 }
83 }
84}
Note: See TracBrowser for help on using the repository browser.