【问题标题】:Why Does my EmailConfirmed property keeps reverting to false after updating and setting it to true为什么我的 EmailConfirmed 属性在更新并将其设置为 true 后一直恢复为 false
【发布时间】:2019-10-30 20:10:37
【问题描述】:

我有一个带有布尔属性 ConfirmEmai 的 StApplications 类,我的应用程序向新用户发送 Confrim 邮件的链接,并在确认后,我将电子邮件确认属性设置为 true 并更新和保存更改。

但我意识到过了一段时间后,我在登录已确认电子邮件的用户时收到此提示,表明该电子邮件未确认。

仅当该用户的电子邮件已确认字段设置为 false 时,才会设置此电子邮件未确认提示。

请问我做错了什么

我已经尝试了很多方法来更新特定用户的 ConfrimEmail 属性,起初它会在一段时间后恢复为 false。

public IActionResult ConfirmEmail(int appId, string token)

    {




        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 并保持不变

【问题讨论】:

    标签: .net asp.net-core-mvc


    【解决方案1】:

    对于您当前的代码,您没有通过在_context.StApplications.Update(applicant); 之后调用await _context.SaveChangesAsync(); 来保存更改。

    另外,你可以试试

        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();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-10
      • 2014-08-01
      • 1970-01-01
      • 2014-06-25
      • 1970-01-01
      • 1970-01-01
      • 2020-11-14
      • 2021-09-28
      相关资源
      最近更新 更多