source: PostgreSqlDotnetCore/Areas/Identity/Pages/Account/ResendEmailConfirmation.cshtml.cs@ 2aea0fd

main
Last change on this file since 2aea0fd 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 [AllowAnonymous]
20 public class ResendEmailConfirmationModel : PageModel
21 {
22 private readonly UserManager<IdentityUser> _userManager;
23 private readonly IEmailSender _emailSender;
24
25 public ResendEmailConfirmationModel(UserManager<IdentityUser> userManager, IEmailSender emailSender)
26 {
27 _userManager = userManager;
28 _emailSender = emailSender;
29 }
30
31 /// <summary>
32 /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
33 /// directly from your code. This API may change or be removed in future releases.
34 /// </summary>
35 [BindProperty]
36 public InputModel Input { get; set; }
37
38 /// <summary>
39 /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
40 /// directly from your code. This API may change or be removed in future releases.
41 /// </summary>
42 public class InputModel
43 {
44 /// <summary>
45 /// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
46 /// directly from your code. This API may change or be removed in future releases.
47 /// </summary>
48 [Required]
49 [EmailAddress]
50 public string Email { get; set; }
51 }
52
53 public void OnGet()
54 {
55 }
56
57 public async Task<IActionResult> OnPostAsync()
58 {
59 if (!ModelState.IsValid)
60 {
61 return Page();
62 }
63
64 var user = await _userManager.FindByEmailAsync(Input.Email);
65 if (user == null)
66 {
67 ModelState.AddModelError(string.Empty, "Verification email sent. Please check your email.");
68 return Page();
69 }
70
71 var userId = await _userManager.GetUserIdAsync(user);
72 var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
73 code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
74 var callbackUrl = Url.Page(
75 "/Account/ConfirmEmail",
76 pageHandler: null,
77 values: new { userId = userId, code = code },
78 protocol: Request.Scheme);
79 await _emailSender.SendEmailAsync(
80 Input.Email,
81 "Confirm your email",
82 $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");
83
84 ModelState.AddModelError(string.Empty, "Verification email sent. Please check your email.");
85 return Page();
86 }
87 }
88}
Note: See TracBrowser for help on using the repository browser.