.net - 为什么我的 EmailConfirmed 属性在更新并将其设置为 true 后一直恢复为 false
问题描述
我有一个带有布尔属性 ConfirmEmai 的 StApplications 类,我的应用程序向新用户发送 Confrim 邮件的链接,并在确认后,我将电子邮件确认属性设置为 true 并更新和保存更改。
但我意识到过了一段时间,我在登录时收到了这个提示,因为用户已经确认了他们的电子邮件,但没有确认该电子邮件。
仅当该用户的电子邮件已确认字段设置为 false 时,才会设置此电子邮件未确认提示。
请问我做错了什么
我已经尝试了很多方法来更新特定用户的 ConfrimEmail 属性,起初它会在一段时间后恢复为 false。
公共 IActionResult ConfirmEmail(int appId,字符串令牌)
{
if (appId == 0 || token == null)
{
return RedirectToAction("ApplicationIndex", "Home");
}
else
{
var applicant = _context.StApplications.SingleOrDefault(c => c.ApplicationId == appId);
if (applicant.tokenProvided == token)
{
applicant.ConfirmEmail = true;
//applicant = new StApplications { IsEmailConfirmed = true };
var updatedApplicant = _ApplicationRepository.Update(applicant);
//_context.StApplications.Update(applicant);
if (updatedApplicant == true)
{
return View();
}
}
else
{
ViewBag.Invalidtoken = "Invalid";
return View();
}
}
return BadRequest();
}
注释掉的部分是我尝试过的,但结果都是一样的,更新为 tru,然后在一段时间后恢复为 false
我希望 ConfirmEmail 操作应将 emailconfirm 属性设置为 true 并保持不变
解决方案
对于您当前的代码,您没有通过调用await _context.SaveChangesAsync();
after来保存更改_context.StApplications.Update(applicant);
。
此外,您可以尝试
public async Task<IActionResult> OnGetAsync(string userId, string code)
{
if (userId == null || code == null)
{
return RedirectToPage("/Index");
}
var user = await _userManager.FindByIdAsync(userId);
if (user == null)
{
return NotFound($"Unable to load user with ID '{userId}'.");
}
code = Encoding.UTF8.GetString(WebEncoders.Base64UrlDecode(code));
var result = await _userManager.ConfirmEmailAsync(user, code);
StatusMessage = result.Succeeded ? "Thank you for confirming your email." : "Error confirming your email.";
return Page();
}
推荐阅读
- python - 如何通过Pycharm等外部应用程序连接docker中的Oracle数据库?
- amazon-web-services - 解析 HTTP 404 响应正文的 Docker AWS ECR 错误:顶级值后的无效字符“p”:“未找到 404 页面\n”
- r - R不使用download.file下载总tar.gz
- php - 如何在 MySQL 中过滤 Select
- asp.net-core - 将字节数组转换为图像 asp.net 核心时出错
- javascript - 在 NgOnInit 和 NgAfterViewInit 上执行的链接任务
- angular - Angular 7 缓存 API 结果
- c++ - 在 C++ 中输出参数与返回值
- azure-iot-central - 在 Azure IoT Central 中筛选来自具有多个用户的设备的遥测数据
- swift - SwiftUI 自定义列表样式