How can I change UserManager logic so that user must exist in database before they register


I am customizing the MVC5 registration process so that when users are registering they must enter two custom fields ‘MyNewField1’ and ‘MyNewField2’ which will be then checked against the user context to ensure if they exist in which case the registration can succeed by updating that current user.

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


src : http://stackoverflow.com/questions/28057451/how-can-i-change-usermanager-logic-so-that-user-must-exist-in-database-before-th

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s