public ActionResult Register(CustomMembershipSample.Models.RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                var manager = HttpContext.GetOwinContext().GetUserManager<AppUserManager>();
                var user = new CustomMembershipSample.IdentityModels.AppUser() { Username = model.UserName, Email = model.Email };
                user.Addresses = new List<Address>();
                user.Addresses.Add(new Address() { City = model.City, State = model.State, Country = model.Country });

                // Create user with supplied credentials
                var result = manager.Create<CustomMembershipSample.IdentityModels.AppUser, int>(user, model.Password);

                if (result == IdentityResult.Success)
                {
                    // On success set the sign in cookie
                    HttpContext.GetOwinContext().Authentication.SignIn(manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie));
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", result.Errors.FirstOrDefault());
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
예제 #2
0
        public ActionResult Register(CustomMembershipSample.Models.RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                var manager = HttpContext.GetOwinContext().GetUserManager <AppUserManager>();
                var user    = new CustomMembershipSample.IdentityModels.AppUser()
                {
                    Username = model.UserName, Email = model.Email
                };
                user.Addresses = new List <Address>();
                user.Addresses.Add(new Address()
                {
                    City = model.City, State = model.State, Country = model.Country
                });

                // Create user with supplied credentials
                var result = manager.Create <CustomMembershipSample.IdentityModels.AppUser, int>(user, model.Password);

                if (result == IdentityResult.Success)
                {
                    // On success set the sign in cookie
                    HttpContext.GetOwinContext().Authentication.SignIn(manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie));
                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    ModelState.AddModelError("", result.Errors.FirstOrDefault());
                }
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }