如何更改UserManager逻辑,以便用户在注册之前必须存在于数据库中

时间:2015-01-21 00:04:48

标签: asp.net-mvc-5 token dbcontext two-factor-authentication usermanager

我正在自定义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);
            }
    }        
}

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

1 个答案:

答案 0 :(得分: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);
    }