forked from OPCFoundation/UA-CloudLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForgotPassword.cshtml.cs
113 lines (99 loc) · 4.56 KB
/
ForgotPassword.cshtml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#nullable disable
using System;
using System.ComponentModel.DataAnnotations;
using System.Text;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Configuration;
using Opc.Ua.Cloud.Library.Authentication;
using Opc.Ua.Cloud.Library.Interfaces;
namespace Opc.Ua.Cloud.Library.Areas.Identity.Pages.Account
{
public class ForgotPasswordModel : PageModel
{
private readonly UserManager<IdentityUser> _userManager;
private readonly IEmailSender _emailSender;
private readonly Interfaces.ICaptchaValidation _captchaValidation;
private readonly CaptchaSettings _captchaSettings;
public ForgotPasswordModel(
UserManager<IdentityUser> userManager,
IEmailSender emailSender,
IConfiguration configuration,
Interfaces.ICaptchaValidation captchaValidation)
{
_userManager = userManager;
_emailSender = emailSender;
_captchaValidation = captchaValidation;
_captchaSettings = new CaptchaSettings();
configuration.GetSection("CaptchaSettings").Bind(_captchaSettings);
}
/// Populate values for cshtml to use
/// </summary>
public CaptchaSettings CaptchaSettings { get { return _captchaSettings; } }
/// <summary>
/// Populate a token returned from client side call to Google Captcha
/// </summary>
[BindProperty]
public string CaptchaResponseToken { get; set; }
/// <summary>
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
[BindProperty]
public InputModel Input { get; set; }
/// <summary>
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public class InputModel
{
/// <summary>
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
[Required]
[EmailAddress]
public string Email { get; set; }
}
public async Task<IActionResult> OnPostAsync()
{
//Captcha validate
var captchaResult = await _captchaValidation.ValidateCaptcha(CaptchaResponseToken);
if (!string.IsNullOrEmpty(captchaResult)) ModelState.AddModelError("CaptchaResponseToken", captchaResult);
if (ModelState.IsValid)
{
var user = await _userManager.FindByEmailAsync(Input.Email).ConfigureAwait(false);
if (user == null || !(await _userManager.IsEmailConfirmedAsync(user).ConfigureAwait(false)))
{
// 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).ConfigureAwait(false);
code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
var callbackUrl = Url.Page(
"/Account/ResetPassword",
pageHandler: null,
values: new { area = "Identity", code },
protocol: Request.Scheme);
//notify user of password reset w/ reset link
await EmailManager.SendPasswordReset(
_emailSender,
Input.Email,
callbackUrl
).ConfigureAwait(false);
return RedirectToPage("./ForgotPasswordConfirmation");
}
return Page();
}
}
}