예제 #1
0
        public ActionResult Register(string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return RedirectToAction("Index", "Home");
            }

            ViewBag.ReturnUrl = returnUrl;

            var viewModel = new LoginRegisterViewModel
            {
                LoginViewModel = new LoginViewModel(),
                RegisterViewModel = new RegisterViewModel()
            };

            return View("Login", viewModel);
        }
예제 #2
0
        public async Task<ActionResult> Register(RegisterViewModel model, string returnUrl)
        {
            LoginRegisterViewModel viewModel;
            bool stripeCustomerScopeSuccessful = false;

            if (ModelState.IsValid)
            {
                var user = new User
                {
                    UserName = model.Username,
                    Email = model.Email,
                    FirstName = model.FirstName,
                    LastName = model.LastName
                };

                IdentityResult result;

                // We need a transaction as we don't want to create the User if we fail to create and
                // save a Stripe customer for them
                using (
                    TransactionScope stripeCustomerScope =
                        new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                {
                    result = await userManager.CreateAsync(user, model.Password);

                    if (result.Succeeded)
                    {
                        string stripeCustomerId;

                        try
                        {
                            stripeCustomerId = stripeService.CreateCustomer(user);
                        }
                        catch (StripeServiceException ex)
                        {
                            if (ex.ExceptionType == StripeExceptionType.ApiKeyError)
                            {
                                return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
                            }

                            ModelState.AddModelError(REGISTER_MODEL_ERRORS_KEY, ex.Message);

                            viewModel = new LoginRegisterViewModel
                            {
                                LoginViewModel = new LoginViewModel(),
                                RegisterViewModel = model
                            };

                            return View("Login", viewModel);
                        }

                        user.Member = new Member
                        {
                            ReceivePromotionalEmails = model.ReceivePromotionalEmail,
                            WishListVisibility = model.WishListVisibility,
                            StripeCustomerId = stripeCustomerId
                        };

                        result = await userManager.UpdateAsync(user);

                        if (result.Succeeded)
                        {
                            // Commit the transaction as we successfully created the Stripe customer and
                            // commited that information in a Member for the user.
                            stripeCustomerScope.Complete();

                            stripeCustomerScopeSuccessful = true;
                        }
                    }
                }

                // We don't need this portion to be inside stripeCustomerScope 
                if (result.Succeeded && stripeCustomerScopeSuccessful)
                {
                    result = await userManager.AddToRoleAsync(user.Id, VeilRoles.MEMBER_ROLE);

                    if (result.Succeeded)
                    {
                        await SendConfirmationEmail(user);

                        return View("RegisterComplete");
                    }
                }

                AddErrors(result, REGISTER_MODEL_ERRORS_KEY);
            }

            ViewBag.ReturnUrl = returnUrl;

            viewModel = new LoginRegisterViewModel
            {
                LoginViewModel = new LoginViewModel(),
                RegisterViewModel = model
            };

            // If we got this far, something failed, redisplay form
            return View("Login", viewModel);
        }
예제 #3
0
        public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                LoginRegisterViewModel viewModel = new LoginRegisterViewModel
                {
                    LoginViewModel = model,
                    RegisterViewModel = new RegisterViewModel()
                };

                return View(viewModel);
            }

            User user = await userManager.FindByEmailAsync(model.LoginEmail);

            SignInStatus result = SignInStatus.Failure;

            if (user != null)
            {
                if (await userManager.CheckPasswordAsync(user, model.LoginPassword))
                {
                    // Require the user to have a confirmed email before they can log on.
                    if (!await userManager.IsEmailConfirmedAsync(user.Id))
                    {
                        return ConfirmResendConfirmationEmail(model.LoginEmail);
                    }

                    await EnsureCorrectRolesAsync(user);

                    // This doesn't count login failures towards account lockout.
                    // This isn't needed as the password is already confirmed as correct.
                    // To enable password failures to trigger account lockout, change to shouldLockout: true
                    result =
                        await signInManager.PasswordSignInAsync(
                            user.UserName, model.LoginPassword, model.RememberMe, shouldLockout: false);
                }
                else
                {
                    // Enable password failures to trigger account lockout
                    await userManager.AccessFailedAsync(user.Id);
                }
            }

            if (result == SignInStatus.Success &&
                await userManager.IsInRoleAsync(user.Id, VeilRoles.MEMBER_ROLE))
            {
                // Set the Cart Quantity in the Session for use in the NavBar
                Session[CartController.CART_QTY_SESSION_KEY] = user.Member.Cart.Items.Count;
            }

            switch (result)
            {
                case SignInStatus.Success:
                    return RedirectToLocal(returnUrl);
                case SignInStatus.LockedOut:
                    return View("Lockout");
                case SignInStatus.Failure:
                default:
                    ModelState.AddModelError(LOGIN_MODEL_ERRORS_KEY, "Invalid login attempt.");

                    LoginRegisterViewModel viewModel = new LoginRegisterViewModel
                    {
                        LoginViewModel = model,
                        RegisterViewModel = new RegisterViewModel()
                    };

                    return View(viewModel);
            }
        }