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 |
|
---|
5 | using System;
|
---|
6 | using System.ComponentModel.DataAnnotations;
|
---|
7 | using System.Text;
|
---|
8 | using System.Threading.Tasks;
|
---|
9 | using Microsoft.AspNetCore.Authorization;
|
---|
10 | using Microsoft.AspNetCore.Identity;
|
---|
11 | using Microsoft.AspNetCore.Mvc;
|
---|
12 | using Microsoft.AspNetCore.Mvc.RazorPages;
|
---|
13 | using Microsoft.AspNetCore.WebUtilities;
|
---|
14 |
|
---|
15 | namespace PostgreSqlDotnetCore.Areas.Identity.Pages.Account
|
---|
16 | {
|
---|
17 | public class ResetPasswordModel : PageModel
|
---|
18 | {
|
---|
19 | private readonly UserManager<IdentityUser> _userManager;
|
---|
20 |
|
---|
21 | public ResetPasswordModel(UserManager<IdentityUser> userManager)
|
---|
22 | {
|
---|
23 | _userManager = userManager;
|
---|
24 | }
|
---|
25 |
|
---|
26 | /// <summary>
|
---|
27 | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
|
---|
28 | /// directly from your code. This API may change or be removed in future releases.
|
---|
29 | /// </summary>
|
---|
30 | [BindProperty]
|
---|
31 | public InputModel Input { get; set; }
|
---|
32 |
|
---|
33 | /// <summary>
|
---|
34 | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
|
---|
35 | /// directly from your code. This API may change or be removed in future releases.
|
---|
36 | /// </summary>
|
---|
37 | public class InputModel
|
---|
38 | {
|
---|
39 | /// <summary>
|
---|
40 | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
|
---|
41 | /// directly from your code. This API may change or be removed in future releases.
|
---|
42 | /// </summary>
|
---|
43 | [Required]
|
---|
44 | [EmailAddress]
|
---|
45 | public string Email { get; set; }
|
---|
46 |
|
---|
47 | /// <summary>
|
---|
48 | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
|
---|
49 | /// directly from your code. This API may change or be removed in future releases.
|
---|
50 | /// </summary>
|
---|
51 | [Required]
|
---|
52 | [StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)]
|
---|
53 | [DataType(DataType.Password)]
|
---|
54 | public string Password { get; set; }
|
---|
55 |
|
---|
56 | /// <summary>
|
---|
57 | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
|
---|
58 | /// directly from your code. This API may change or be removed in future releases.
|
---|
59 | /// </summary>
|
---|
60 | [DataType(DataType.Password)]
|
---|
61 | [Display(Name = "Confirm password")]
|
---|
62 | [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
|
---|
63 | public string ConfirmPassword { get; set; }
|
---|
64 |
|
---|
65 | /// <summary>
|
---|
66 | /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
|
---|
67 | /// directly from your code. This API may change or be removed in future releases.
|
---|
68 | /// </summary>
|
---|
69 | [Required]
|
---|
70 | public string Code { get; set; }
|
---|
71 |
|
---|
72 | }
|
---|
73 |
|
---|
74 | public IActionResult OnGet(string code = null)
|
---|
75 | {
|
---|
76 | if (code == null)
|
---|
77 | {
|
---|
78 | return BadRequest("A code must be supplied for password reset.");
|
---|
79 | }
|
---|
80 | else
|
---|
81 | {
|
---|
82 | Input = new InputModel
|
---|
83 | {
|
---|
84 | Code = Encoding.UTF8.GetString(WebEncoders.Base64UrlDecode(code))
|
---|
85 | };
|
---|
86 | return Page();
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | public async Task<IActionResult> OnPostAsync()
|
---|
91 | {
|
---|
92 | if (!ModelState.IsValid)
|
---|
93 | {
|
---|
94 | return Page();
|
---|
95 | }
|
---|
96 |
|
---|
97 | var user = await _userManager.FindByEmailAsync(Input.Email);
|
---|
98 | if (user == null)
|
---|
99 | {
|
---|
100 | // Don't reveal that the user does not exist
|
---|
101 | return RedirectToPage("./ResetPasswordConfirmation");
|
---|
102 | }
|
---|
103 |
|
---|
104 | var result = await _userManager.ResetPasswordAsync(user, Input.Code, Input.Password);
|
---|
105 | if (result.Succeeded)
|
---|
106 | {
|
---|
107 | return RedirectToPage("./ResetPasswordConfirmation");
|
---|
108 | }
|
---|
109 |
|
---|
110 | foreach (var error in result.Errors)
|
---|
111 | {
|
---|
112 | ModelState.AddModelError(string.Empty, error.Description);
|
---|
113 | }
|
---|
114 | return Page();
|
---|
115 | }
|
---|
116 | }
|
---|
117 | }
|
---|