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

时间:2018-03-28 05:47:26

标签: asp.net-identity

任何人都可以帮助我解决这个问题,
   在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); 

在此处验证时始终显示无效令牌              return view(“views / Shared / ErrorModal.cshtml”);         }

1 个答案:

答案 0 :(得分:1)

是的,我得到了答案,因为在创建用户时安全标记为空,

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