Esempio n. 1
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName                         = model.Email,
                    Email                            = model.Email,
                    Name                             = model.Name,
                    PhoneNumber                      = model.PrimaryPhoneNumber,
                    SecondaryPhoneNumber             = model.SecondaryPhoneNumber,
                    PreferredCommunicationMethod     = model.PreferredCommunicationMethod,
                    DefaultAddress                   = model.AddressName,
                    UnsubscribeFromAllCorrespondence = false,
                    UnsubscribeFromRatings           = false,
                    LastVisited                      = DateTime.Now
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    CustomerAddress.AddAddress(user.Id, model.AddressName, model.AddressLine1, model.AddressLine2
                                               , model.AddressLine3, model.AddressLine4, model.AddressSuburb
                                               , model.AddressCity, model.AddressPostalCode);

                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    SetShoppingCartSession(user.Id);

                    string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

                    var    callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    string html        = "";
                    string message     = "";
                    using (FreeMarketEntities db = new FreeMarketEntities())
                    {
                        html    = db.SiteConfigurations.First(c => c.Key == "Email: Confirm Account")?.Value;
                        message = html.Replace("{{0}}", user.Name).Replace("{{1}}", callbackUrl);
                    }
                    await UserManager.SendEmailAsync(user.Id, "Confirm your account", message);

                    AuditUser.LogAudit(1, "", user.Id);

                    return(RedirectToAction("Index", "Home"));

                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
        public ActionResult ModifyDeliveryDetails(ModifyDeliveryDetailsViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = UserManager.FindById(User.Identity.GetUserId());
                if (user == null)
                {
                    return(View("Error"));
                }

                if (CustomerAddress.AddressExists(user.Id, model.AddressName))
                {
                    FreeMarketResult result = CustomerAddress.UpdateAddress(user.Id, model.AddressName, model.Address.AddressLine1, model.Address.AddressLine2
                                                                            , model.Address.AddressLine3, model.Address.AddressLine4, model.Address.AddressSuburb
                                                                            , model.Address.AddressCity, model.Address.AddressPostalCode);

                    if (result == FreeMarketResult.Success)
                    {
                        TempData["message"] = string.Format
                                                  ("Your {0} address has been updated.",
                                                  model.AddressName);
                    }
                    else
                    {
                        TempData["message"] = string.Format
                                                  ("Sorry, we could not process your request at this time, please try again later.");
                    }
                }
                else
                {
                    FreeMarketResult result = CustomerAddress.AddAddress(user.Id, model.AddressName, model.Address.AddressLine1, model.Address.AddressLine2
                                                                         , model.Address.AddressLine3, model.Address.AddressLine4, model.Address.AddressSuburb
                                                                         , model.Address.AddressCity, model.Address.AddressPostalCode);

                    if (result == FreeMarketResult.Success)
                    {
                        TempData["message"] = string.Format
                                                  ("Your {0} address has been added to our system.",
                                                  model.AddressName);
                    }
                    else
                    {
                        TempData["message"] = string.Format
                                                  ("Sorry, we could not process your request at this time, please try again later.");
                    }
                }

                return(RedirectToAction("Index"));
            }

            return(View(model));
        }
Esempio n. 3
0
        public async Task <ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl, string loginProvider)
        {
            if (User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Index", "Manage"));
            }

            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();

                if (info == null)
                {
                    return(View("ExternalLoginFailure"));
                }

                var user = new ApplicationUser
                {
                    UserName                         = model.Email,
                    Email                            = model.Email,
                    Name                             = model.Name,
                    PhoneNumber                      = model.PrimaryPhoneNumber,
                    SecondaryPhoneNumber             = model.SecondaryPhoneNumber,
                    PreferredCommunicationMethod     = model.PreferredCommunicationMethod,
                    DefaultAddress                   = model.AddressName,
                    UnsubscribeFromAllCorrespondence = false,
                    UnsubscribeFromRatings           = false,
                    LastVisited                      = DateTime.Now
                };


                var result = await UserManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);

                    if (result.Succeeded)
                    {
                        CustomerAddress.AddAddress(user.Id, model.AddressName, model.AddressLine1, model.AddressLine2
                                                   , model.AddressLine3, model.AddressLine4, model.AddressSuburb
                                                   , model.AddressCity, model.AddressPostalCode);

                        await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                        SetShoppingCartSession(user.Id);

                        AuditUser.LogAudit(1, "", user.Id);

                        return(RedirectToLocal(returnUrl));
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl     = returnUrl;
            ViewBag.LoginProvider = loginProvider;

            return(View(model));
        }