首页 > 解决方案 > 为什么我的 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 并保持不变

标签: .netasp.net-core-mvc

解决方案


对于您当前的代码,您没有通过调用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();
    }

推荐阅读