ASP MVC核心身份电子邮件确认



任何人都可以在这个问题上帮助我,
在ASP.NET核心身份中,我尝试通过遵循此代码来通过电子邮件确认创建帐户创建,它总是显示无效的令牌在确认代币时,我还搜索了所有相关答案,它们都没有帮助我

enter code here
      public async Task<IActionResult> RegisterUser(RegisterViewModel registerModel)
    {
        if (Request.Method=="POST")
        {
            if (ModelState.IsValid)
            {
                ErrorModals er = new ErrorModals();
                var userName = _userManager.FindByEmailAsync(registerModel.EmailAddress).Result;
                if (userName == null)
                {
                    var userDetails = new ApplicationUser {UserName= registerModel.FirstName, Email=registerModel.EmailAddress };
                    var dd = await _userManager.CreateAsync(new ApplicationUser()
                    {
                        UserName = (registerModel.FirstName).Trim(),
                        Email = (registerModel.EmailAddress).Trim(),
                    }, registerModel.Password);
                    var tokenCode = await _signManager.UserManager.GenerateEmailConfirmationTokenAsync(userDetails);               
                    var res = _signManager.UserManager.ConfirmEmailAsync(userDetails, tokenCode).Result;
                    tokenCode = System.Web.HttpUtility.UrlEncode(tokenCode);
                    userName = _userManager.FindByEmailAsync(registerModel.EmailAddress).Result;                 
                var callBackUrl = Url.Action("ConfirmEmail", "Account", new { userId=userName.Id,code=tokenCode}, protocol: HttpContext.Request.Scheme);
                    MailModel mailModel = new MailModel();
                    EmailProcess emailProcess = new EmailProcess();
                    mailModel.Email = registerModel.EmailAddress;
                    mailModel.Subject = "Email Confirmation";
                    await emailProcess.SendMailAsync(mailModel, callBackUrl);                     
                   return RedirectToAction("Login", "Account", "");
                }
                else
                {
                    er.ErrorMessage = "Email  Already Registerd.";
                    er.LinkMessage = "Click To Register Again";
                    er.Action = "Account";
                    er.Controller = "createuser";
                  //  ModelState.AddModelError(string.Empty, "Email  Already Registerd.");
                    return View("views/Shared/ErrorModal.cshtml",er);
                }
            }
            return View("views/userauthenticate/register.cshtml");
        }
       ModelState.Clear();
        return View("views/userauthenticate/register.cshtml");
    }
 public async Task <IActionResult> ConfirmEmail(string userId, string code)
    {
             ErrorModals models = new ErrorModals();
        var encodedCode = System.Net.WebUtility.UrlDecode(code);  
        var userDetails = _signInManager.UserManager.FindByIdAsync(userId).Result;           
        var response = await _signInManager.UserManager.ConfirmEmailAsync(userDetails, encodedCode); 

它总是显示无效的令牌,而在此处进行验证 返回视图("视图/共享/errormodal.cshtml"(; }

是的,我得到了答案,这是由于安全邮票而在创建用户时为null,

 var userDetails = new ApplicationUser { UserName = (registerModel.FirstName).Trim(), Email = (registerModel.EmailAddress).Trim() };
                    var isCreateUser = await _userManager.CreateAsync(userDetails, registerModel.Password);

相关内容

  • 没有找到相关文章