0

我正在自定义 MVC5 注册过程,因此当用户注册时,他们必须输入两个自定义字段“MyNewField1”和“MyNewField2”,然后将根据用户上下文检查它们是否存在,在这种情况下,通过更新注册可以成功当前用户。

 public async Task<ActionResult> CustomRegister(CustomRegisterViewModel model)
  {
        if (ModelState.IsValid)
        {                
            var context = new ApplicationDbContext();
            ApplicationUser user = context.Users.Where(a => a.MyNewField1== model.MyNewField1& a.MyNewField2== a.MyNewField2).SingleOrDefault();

            if(user != null)
            {
                var emailCheck = await UserManager.FindByNameAsync(model.Email);

                if (emailCheck == null)
                {
                    //We have found a user and email address has not been already assigned to another
                    //assign the email entered for this user in place of the username and email place
                    //holders and update the user before saving to the database
                    user.UserName = model.Email;
                    user.Email = model.Email;
                    var hasher = new PasswordHasher();
                    user.PasswordHash = hasher.HashPassword(model.Password);
                    context.SaveChanges();

                    var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    await UserManager.SendEmailAsync(user.Id, "Budget Energy Email Verification", "Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>");
                    ViewBag.Link = callbackUrl;

                    ViewBag.Message = "Check your email and confirm your account, you must be confirmed before you can log in.";
                    return View("Info");

                }
                else
                {
                    //This email address is already assigned to a user
                    return View(model);
                }
            }
            else
            {
                //No user exists with these details so redisplay form
                return View(model);
            }
    }        
}

此方法成功通过,我被告知已发送一封电子邮件,但是当我单击此电子邮件链接时,我被带到一个错误页面,错误为 Invalid Token。因为我已经改变了这里的逻辑,我是否必须以不同的方式创建令牌?

4

1 回答 1

0

我能够解决这个问题如下:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> BillpayRegister(BillpayRegisterViewModel model)
{
  if (ModelState.IsValid)
  {                
    var context = new ApplicationDbContext();
    ApplicationUser customer = context.Users.Where(a => a.MyNewField1 == model.MyNewField1 & a.MyNewField2 == model.MyNewField2).SingleOrDefault();

            if(customer != null)
            {
                var emailCheck = await UserManager.FindByNameAsync(model.Email);

                if (emailCheck == null)
                {
                    //We have found a user and email address has not been already assigned to another
                    //assign the email entered for this user in place of the username and email place
                    //holders and update the user before saving to the database
                    var user = UserManager.FindById(customer.Id);
                    user.UserName = model.Email;
                    UserManager.SetEmail(user.Id, model.Email);                        
                    string hashedNewPassword = UserManager.PasswordHasher.HashPassword(model.Password);
                    user.PasswordHash = hashedNewPassword;
                    var result = UserManager.Update(user); 
                    if (result.Succeeded)
                    {
                        var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                        var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                        await UserManager.SendEmailAsync(user.Id, "Email Verification", "Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>");
                        ViewBag.Link = callbackUrl;

                        ViewBag.Message = "Check your email and confirm your account, you must be confirmed before you can log in.";
                        return View("Info");
                    }                                                
                }
                else
                {
                    //This email address is already assigned to a user
                    return View(model);
                }
            }
            else
            {
                //No user exists with these details so redisplay form
                return View(model);
            }
        }
        // If we got this far, something failed, redisplay form
        return View(model);
    }
于 2015-02-02T00:58:47.770 回答