コード例 #1
0
        [ValidateAntiForgeryToken]//created a new view for Register - overriding the old view
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    #region Assigning customer a role of user and Creating User Object

                    //Added to assign customers a role of user when registering.
                    UserManager.AddToRole(user.Id, "User");
                    //***********  USER INFO

                    //Create user object as well
                    UserDetail info = new UserDetail()
                    {
                        UserID    = user.Id,
                        FirstName = model.FirstName,
                        LastName  = model.LastName,
                    };
                    //Create the context to get to EF
                    PawsNClawsEntities ctx = new PawsNClawsEntities();
                    //add the UserDetails Object to EF
                    ctx.UserDetails.Add(info);
                    //Sent the object to the database as a record
                    ctx.SaveChanges();
                    #endregion

                    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,
                                                     "Confirm your account", "Please confirm your account by clicking this link: <a href=\""
                                                     + callbackUrl + "\">link</a>");

                    ViewBag.Link = callbackUrl;
                    return(View("DisplayEmail"));
                }
                AddErrors(result);
            }
            // If we got this far, something failed, redisplay form
            return(View(model));
        }