use*_*329 2 .net c# asp.net-mvc-4
我正在使用以下代码生成重置令牌
var token = WebSecurity.GeneratePasswordResetToken(user);
Run Code Online (Sandbox Code Playgroud)
它第一次工作,因为它更新了 pages_Membership 表,并更新了以下字段,
PasswordVerificationToken PasswordVerificationTokenExpirationDate
但是当我再次运行上面的代码时,返回的令牌是相同的,并且没有任何更新。
它是如何工作的?
令牌仅在与 一起使用后才会刷新WebSecurity.ResetPassword(model.ResetToken, model.TheUsersNewPassword);。如果您没有完成完整的密码重置过程,将始终为该特定用户生成相同的令牌。
如果在 24 小时内(默认)未重置密码,则上述方法将返回false。如果要覆盖默认密码过期时间,可以tokenExpirationInMinutesFromNow在调用时添加可选参数WebSecurity.GeneratePasswordResetToken:
public static string GeneratePasswordResetToken(
string userName,
int tokenExpirationInMinutesFromNow
)
Run Code Online (Sandbox Code Playgroud)
如果您想捕获错误的密码更改尝试,只需包含WebSecurity.ResetPassword(model.ResetToken, model.TheUsersNewPassword);在 try catch 块中,您就可以向用户显示错误:
[HttpPost, AllowAnonymous, ValidateAntiForgeryToken]
public ActionResult ForgotPassword(ForgotPasswordModel model)
{
if (ModelState.IsValid)
{
try
{
//Reset password using the reset token and the new password
WebSecurity.ResetPassword(model.ResetToken, model.TheUsersNewPassword);
//Redirect to the home account page.
return RedirectToAction("Index", "Home");
}
catch (Exception ex)
{
ModelState.AddModelError(string.Empty, LocalizedText.Account_Reset_Password_Error);
}
}
//Something bad happen, notify the user
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
我不会太担心在此处捕获特定异常,因为无论哪种方式修复都是相同的 - 他们需要生成另一个密码重置令牌。
| 归档时间: |
|
| 查看次数: |
4862 次 |
| 最近记录: |