经过一番搜索,感谢马文的回应,我来到了下面的代码。也许它可以帮助任何搜索这个主题的人。开放改进...
我在开发中的服务器端应用程序中对此进行了测试。不在iis上
了解 usermanager
kudvenkat 的链接,这是 .net 核心上很棒的 youtube 系列
剃须刀组件
@page "/ResetPassword"
@inject UserManager<IdentityUser> userManager
@inject NavigationManager navigationManager
@inject IDataProtectionProvider dataProtectionProvider
@using Microsoft.AspNetCore.WebUtilities;
@using System.Text.Encodings.Web;
@using System.Text;
@* THIS IS THE PART HANDLING THE RESET TOKEN INCOMING VALIDATION *@
@if (IsResetToken)
{
<h3>Reset Password</h3>
<div>
Please enter a new Password bellow;
</div>
<br />
<EditForm Model="@validatePasswordModel" OnValidSubmit="@SetResetPassword">
<DataAnnotationsValidator />
<ValidationSummary />
<div class="field">
<label class="label">Password</label>
<div class="control">
<InputText id="password" class="input" type="password" @bind-Value="@validatePasswordModel.Password"></InputText>
</div>
</div>
<div class="field">
<label class="label">Confirm password</label>
<div class="control">
<InputText id="cpassword" class="input" type="password" @bind-Value="@validatePasswordModel.ConfirmPassword"></InputText>
</div>
</div>
<div class="field">
<div class="control">
<button type="submit" class="button is-primary">Set Password</button>
</div>
</div>
</EditForm>
<br />
<a href="/">Back</a>
}
else
{
@* THIS IS THE PART HANDLING THE RESET TOKEN OUTGOING VALIDATION *@
@if (IsResetSend)
{
<h3>Reset Password</h3>
<div>
A mail has been send with a password reset link, please click this link to set a new password.
</div>
<br />
<a href="/">Back</a>
}
else
{
<h3>Reset Password</h3>
<EditForm Model="requestPasswordModel" OnValidSubmit="@SendResetPassword">
<div class="field">
<label class="label">E-mail</label>
<div class="control">
<InputText id="email" class="input" type="email" @bind-Value="@requestPasswordModel.Email"></InputText>
</div>
</div>
<div class="field">
<div class="control">
<button type="submit" class="button is-primary">Sign in</button>
</div>
</div>
</EditForm>
}
}
@code {
InputModel requestPasswordModel = new InputModel();
ResetPasswordModel validatePasswordModel = new ResetPasswordModel();
string email = "";
string token = "";
bool IsResetSend = false;
bool IsResetToken = false;
protected override void OnInitialized()
{
GetHttpParameters();
}
public void GetHttpParameters()
{
var uriBuilder = new UriBuilder(navigationManager.Uri);
var httpQuery = System.Web.HttpUtility.ParseQueryString(uriBuilder.Query);
email = httpQuery["email"] ?? "";
token = httpQuery["token"] ?? "";
if (!string.IsNullOrEmpty(email) && !string.IsNullOrEmpty(token))
{
IsResetToken = true;
}
}
public async void SendResetPassword()
{
List<string> mailTo = new List<string>();
string passwordResetToken;
string passwordResetUrl;
var user = await userManager.FindByEmailAsync(requestPasswordModel.Email);
if (user != null)
{
passwordResetUrl = navigationManager.BaseUri;
passwordResetToken = await userManager.GeneratePasswordResetTokenAsync(user);
passwordResetToken = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(passwordResetToken));
passwordResetUrl = "https://localhost:44384/ResetPassword?email=" + user.Email + "&token=" + passwordResetToken;
mailTo.Add(requestPasswordModel.Email);
//Send mail with the relevant token
Webco.Email.Entities.EmailObject emailObject = new Webco.Email.Entities.EmailObject();
emailObject.Email_To = mailTo;
emailObject.Email_Subject = "Password Reset Webco !";
emailObject.Email_Message = $"Hy There, <br> Please click this link to reset your password : <br><a href='{HtmlEncoder.Default.Encode(passwordResetUrl)}'> clikcing here </a>.";
emailObject.SendEmail(emailObject);
IsResetSend = true;
}
else
{
//it's a lie
IsResetSend = true;
}
}
public async void SetResetPassword()
{
List<string> mailTo = new List<string>();
var user = await userManager.FindByEmailAsync(email);
if (user != null)
{
token = System.Text.Encoding.UTF8.GetString(WebEncoders.Base64UrlDecode(token));
var result = await userManager.ResetPasswordAsync(user, token, validatePasswordModel.ConfirmPassword);
if (result.Succeeded)
{
mailTo.Add(email);
//Send mail with the relevant token
Webco.Email.Entities.EmailObject emailObject = new Webco.Email.Entities.EmailObject();
emailObject.Email_To = mailTo;
emailObject.Email_Subject = "Password Reset Webco !";
emailObject.Email_Message = $"Hy There, <br> You password was just reset at webco. Not you ? <br> Contact webco for support.";
emailObject.SendEmail(emailObject);
navigationManager.NavigateTo("/LoginUserResetPasswordComplete", true);
}
else
{
navigationManager.NavigateTo("/LoginUserResetPasswordError", true);
}
}
else
{
navigationManager.NavigateTo("/LoginUserResetPasswordError",true);
}
}
public class InputModel
{
[Required]
[EmailAddress]
public string Email { get; set; }
}
public class ResetPasswordModel
{
[Required]
public string Password { get; set; }
[Required]
[Compare(nameof(Password))]
public string ConfirmPassword { get; set; }
}
}