public ActionResult JsonRegister(UserFormModel form)
        {
            if (ModelState.IsValid)
            {
                var command = new UserRegisterCommand
                {
                    FirstName = form.FirstName,
                    LastName = form.LastName,
                    Email = form.Email,
                    Password = form.Password,
                    Activated = true,
                    RoleId = (Int32)UserRoles.User
                };
                IEnumerable<ValidationResult> errors = commandBus.Validate(command);
                ModelState.AddModelErrors(errors);
                if (ModelState.IsValid)
                {
                    var result = commandBus.Submit(command);
                    if (result.Success)
                    {
                        User user = userRepository.Get(u => u.Email == form.Email);
                        formAuthentication.SetAuthCookie(this.HttpContext,
                                                          UserAuthenticationTicketBuilder.CreateAuthenticationTicket(
                                                              user));
                        return Json(new { success = true });
                    }
                    else
                    {
                        ModelState.AddModelError("", "An unknown error occurred.");
                    }
                }
                // If we got this far, something failed
                return Json(new { errors = GetErrorsFromModelState() });
            }

            // If we got this far, something failed
            return Json(new { errors = GetErrorsFromModelState() });
        }
        public ActionResult Register(UserFormModel model)
        {
            if (ModelState.IsValid)
            {
                var command = new UserRegisterCommand
                {
                    FirstName = model.FirstName,
                    LastName = model.LastName,
                    Email = model.Email,
                    Password = model.Password,
                    Activated = true,
                    RoleId = (Int32)UserRoles.User
                };

                IEnumerable<ValidationResult> errors = commandBus.Validate(command);
                ModelState.AddModelErrors(errors);
                if (ModelState.IsValid)
                {
                    var result = commandBus.Submit(command);
                    if (result.Success)
                    {
                        User user = userRepository.Get(u => u.Email == model.Email);
                        formAuthentication.SetAuthCookie(this.HttpContext, UserAuthenticationTicketBuilder.CreateAuthenticationTicket(user));
                        return RedirectToAction("Index", "Home");
                    }
                    else
                    {
                        ModelState.AddModelError("", "An unknown error occurred.");
                    }
                }
                // If we got this far, something failed, redisplay form
                return View(model);
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
Exemple #3
0
        public void UserCreateTest()
        {
            using (var lifetime = container.BeginLifetimeScope())
            {
                DefaultCommandBus commandBus = lifetime.Resolve<DefaultCommandBus>();

                User user = new User()
                {
                    FirstName = "Test",
                    LastName = "User",
                    Email = "*****@*****.**",
                    DateCreated = DateTime.Now,
                    RoleId = 1,
                    Activated = true
                };

                UserRegisterCommand command = new UserRegisterCommand(user, "TEST");
                IValidationHandler<UserRegisterCommand> validationHandler = lifetime.Resolve<IValidationHandler<UserRegisterCommand>>();
                IEnumerable<ValidationResult> validations = commandBus.Validate(command, validationHandler);
                foreach (var val in validations)
                {
                    Assert.IsNull(val, "Error: User creation did not validate " + val.Message);
                }
                ICommandHandler<UserRegisterCommand> commnadHandler = lifetime.Resolve<ICommandHandler<UserRegisterCommand>>();
                ICommandResult result = commandBus.Submit(command, commnadHandler);
                Assert.IsNotNull(result, "Error: User was not created by CommandBus");
                Assert.IsTrue(result.Success, "Error: User was not created by CommandBus");
            }
        }