Beispiel #1
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            StoreFront storeFront = CurrentStoreFrontOrNull;
            StoreFrontConfiguration storeFrontConfig = CurrentStoreFrontConfigOrNull;

            if ((storeFront != null) && (storeFrontConfig != null) && (storeFrontConfig.RegisterWebForm != null) && storeFrontConfig.RegisterWebForm.IsActiveBubble())
            {
                FormProcessorExtensions.ValidateFields(this, storeFrontConfig.RegisterWebForm);
            }

            if (ModelState.IsValid)
            {
                var user = new AspNetIdentityUser(model.Email)
                {
                    UserName = model.Email, Email = model.Email
                };
                user.TwoFactorEnabled = Settings.IdentityEnableTwoFactorAuth;
                IdentityResult result = null;
                try
                {
                    result = await UserManager.CreateAsync(user, model.Password);
                }
                catch (System.Data.Entity.Validation.DbEntityValidationException exDbEx)
                {
                    foreach (System.Data.Entity.Validation.DbEntityValidationResult valResult in exDbEx.EntityValidationErrors)
                    {
                        ICollection <System.Data.Entity.Validation.DbValidationError> valErrors = valResult.ValidationErrors;
                        foreach (System.Data.Entity.Validation.DbValidationError error in valErrors)
                        {
                            ModelState.AddModelError(error.PropertyName, error.ErrorMessage);
                        }
                    }
                    return(View(model));
                }
                catch (Exception ex)
                {
                    string error = ex.ToString();
                    throw;
                }
                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent : true, rememberBrowser : false);

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

                    IGstoreDb   ctx        = GStoreDb;
                    UserProfile newProfile = ctx.UserProfiles.Create();
                    newProfile.UserId   = user.Id;
                    newProfile.UserName = user.UserName;
                    newProfile.Email    = user.Email;
                    newProfile.FullName = model.FullName;
                    newProfile.NotifyOfSiteUpdatesToEmail = model.NotifyOfSiteUpdates;
                    newProfile.SendMoreInfoToEmail        = model.SendMeMoreInfo;
                    newProfile.SignupNotes           = model.SignupNotes;
                    newProfile.NotifyAllWhenLoggedOn = true;
                    newProfile.IsPending             = false;
                    newProfile.Order            = CurrentStoreFrontOrThrow.UserProfiles.Max(up => up.Order) + 10;
                    newProfile.EntryDateTime    = Session.EntryDateTime().Value;
                    newProfile.EntryRawUrl      = Session.EntryRawUrl();
                    newProfile.EntryReferrer    = Session.EntryReferrer();
                    newProfile.EntryUrl         = Session.EntryUrl();
                    newProfile.StartDateTimeUtc = DateTime.UtcNow.AddMinutes(-1);
                    newProfile.EndDateTimeUtc   = DateTime.UtcNow.AddYears(100);
                    newProfile.StoreFrontId     = CurrentStoreFrontOrThrow.StoreFrontId;
                    newProfile.StoreFront       = CurrentStoreFrontOrThrow;
                    newProfile.ClientId         = this.CurrentClientOrThrow.ClientId;
                    newProfile.Client           = this.CurrentClientOrThrow;
                    newProfile = ctx.UserProfiles.Add(newProfile);
                    ctx.SaveChanges();

                    ctx.UserName          = user.UserName;
                    ctx.CachedUserProfile = null;


                    string customFields = string.Empty;
                    if (storeFrontConfig != null && storeFrontConfig.RegisterWebForm != null && storeFrontConfig.RegisterWebForm.IsActiveBubble())
                    {
                        FormProcessorExtensions.ProcessWebForm(this, storeFrontConfig.RegisterWebForm, null, true, null);
                        customFields = FormProcessorExtensions.BodyTextCustomFieldsOnly(this, storeFrontConfig.RegisterWebForm);
                    }

                    bool confirmResult = SendEmailConfirmationCode(user.Id, newProfile);

                    ctx.LogSecurityEvent_NewRegister(this.HttpContext, RouteData, newProfile, this);
                    string notificationBaseUrl = Url.Action("Details", "Notifications", new { id = "" });
                    CurrentStoreFrontOrThrow.HandleNewUserRegisteredNotifications(this.GStoreDb, Request, newProfile, notificationBaseUrl, true, true, customFields);

                    if (storeFront != null)
                    {
                        Cart cart = storeFront.GetCart(Session.SessionID, null);
                        cart = storeFront.MigrateCartToProfile(GStoreDb, cart, newProfile, this);
                        storeFront.MigrateOrdersToNewProfile(GStoreDb, newProfile, this);
                    }

                    if (Settings.IdentityEnableNewUserRegisteredBroadcast && CurrentClientOrThrow.EnableNewUserRegisteredBroadcast)
                    {
                        string title   = model.FullName;
                        string message = "Newly registered!";
                        Microsoft.AspNet.SignalR.IHubContext hubCtx = Microsoft.AspNet.SignalR.GlobalHost.ConnectionManager.GetHubContext <GStoreWeb.Hubs.NotifyHub>();
                        hubCtx.Clients.All.addNewMessageToPage(title, message);
                    }

                    if (model.CheckingOut ?? false)
                    {
                        return(RedirectToAction("LoginOrGuest", "Checkout", new { ContinueAsLogin = true }));
                    }

                    if (storeFrontConfig != null && storeFrontConfig.RegisterSuccess_PageId.HasValue)
                    {
                        return(Redirect(storeFrontConfig.RegisterSuccessPage.UrlResolved(this.Url)));
                    }
                    return(RedirectToAction("RegisterSuccess"));
                }
                AddErrors(result);
            }

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