我正在实现在ASP.NET Identity中使用AccountController的ForgotPassword功能,与标准的VS 2015项目模板相同。
我要解决的问题是,在发送密码重置电子邮件时,页面响应会有明显的延迟。如果密码恢复尝试未找到现有帐户,则不会发送电子邮件,因此响应更快。因此,我认为可以利用这种明显的延迟进行帐户枚举,即黑客可以根据忘记密码页面的响应时间确定帐户是否存在。
因此,我想消除页面响应时间上的差异,以便无法检测到是否找到了帐户。
过去,我使用了类似于以下代码的方法将潜在缓慢的任务(如发送电子邮件)排队到后台线程中:
ThreadPool.QueueUserWorkItem(new WaitCallback(AccountNotification.SendPasswordResetLink),
notificationInfo);
但是在.NET Core中不存在ThreadPool.QueueUserWorkItem,因此我需要一些替代方案。
我想一个想法是在找不到账户的情况下引入人工延迟,使用Thread.Sleep,但我更希望找到一种发送电子邮件而不阻塞UI的方法。
更新:为了澄清问题,我发布实际代码:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> ForgotPassword(ForgotPasswordViewModel model)
{
if (ModelState.IsValid)
{
var user = await userManager.FindByNameAsync(model.Email);
if (user == null || !(await userManager.IsEmailConfirmedAsync(user)))
{
// Don't reveal that the user does not exist or is not confirmed
return View("ForgotPasswordConfirmation");
}
var code = await userManager.GeneratePasswordResetTokenAsync(user);
var resetUrl = Url.Action("ResetPassword", "Account",
new { userId = user.Id, code = code },
protocol: HttpContext.Request.Scheme);
//there is a noticeable delay in the UI here because we are awaiting
await emailSender.SendPasswordResetEmailAsync(
userManager.Site,
model.Email,
"Reset Password",
resetUrl);
return View("ForgotPasswordConfirmation");
}
// If we got this far, something failed, redisplay form
return View(model);
}
有没有其他内置框架功能可以处理这个问题的好方法?