UserManager:如何检查是否使用了 GenerateChangeEmailTokenAsync() 生成的代码
UserManager: How to check if the code generated by GenerateChangeEmailTokenAsync() was used
我使用 UserManager 的 GenerateChangeEmailTokenAsync()
生成验证码并将其发送到新邮箱。我想检查该代码是否已在未经身份验证的上下文中使用,如果新电子邮件已经过验证,则向用户显示一条消息:“您的新电子邮件地址已经过验证。请登录”。
UserManager 中是否有方法可以完成此操作?
恐怕userManager
中没有方法可以达到你的要求,但我想你可以自己传递这个成功信息。可以参考这段代码:
//I use this code to get the currently logged in user
var user = _userManager.GetUserAsync(User).Result;
var result = await _userManager.ChangeEmailAsync(user, newEmail, token);
if (result.Succeeded)
{
TempData["Message"] = "Your new email address has already been verified. Please log in";
return RedirectToAction("ChangeEmail", "Test");
}
else
{
TempData["Message"] = "Email change failed";
return RedirectToAction("ChangeEmail", "Test");
}
Test/ChangeEmail
[HttpGet]
public IActionResult ChangeEmail()
{
//use ViewBag to pass data from controller to view
ViewBag.Message = TempData["Message"];
return View();
}
查看
//.....
<h1>@ViewBag.Message</h1>
如果更改邮箱成功,会在视图中显示消息
我使用 UserManager 的 GenerateChangeEmailTokenAsync()
生成验证码并将其发送到新邮箱。我想检查该代码是否已在未经身份验证的上下文中使用,如果新电子邮件已经过验证,则向用户显示一条消息:“您的新电子邮件地址已经过验证。请登录”。
UserManager 中是否有方法可以完成此操作?
恐怕userManager
中没有方法可以达到你的要求,但我想你可以自己传递这个成功信息。可以参考这段代码:
//I use this code to get the currently logged in user
var user = _userManager.GetUserAsync(User).Result;
var result = await _userManager.ChangeEmailAsync(user, newEmail, token);
if (result.Succeeded)
{
TempData["Message"] = "Your new email address has already been verified. Please log in";
return RedirectToAction("ChangeEmail", "Test");
}
else
{
TempData["Message"] = "Email change failed";
return RedirectToAction("ChangeEmail", "Test");
}
Test/ChangeEmail
[HttpGet]
public IActionResult ChangeEmail()
{
//use ViewBag to pass data from controller to view
ViewBag.Message = TempData["Message"];
return View();
}
查看
//.....
<h1>@ViewBag.Message</h1>
如果更改邮箱成功,会在视图中显示消息