public void Register_Post_ReturnsViewIfRegistrationFails()
        {
            // 排列
            AccountController controller = GetAccountController();
            RegisterModel model = new RegisterModel()
            {
                UserName = "******",
                Email = "goodEmail",
                Password = "******",
                ConfirmPassword = "******"
            };

            // 操作
            ActionResult result = controller.Register(model);

            // 断言
            Assert.IsInstanceOfType(result, typeof(ViewResult));
            ViewResult viewResult = (ViewResult)result;
            Assert.AreEqual(model, viewResult.ViewData.Model);
            Assert.AreEqual("用户名已存在。请另输入一个用户名。", controller.ModelState[""].Errors[0].ErrorMessage);
            Assert.AreEqual(10, viewResult.ViewData["PasswordLength"]);
        }
        public void Register_Post_ReturnsRedirectOnSuccess()
        {
            // 排列
            AccountController controller = GetAccountController();
            RegisterModel model = new RegisterModel()
            {
                UserName = "******",
                Email = "goodEmail",
                Password = "******",
                ConfirmPassword = "******"
            };

            // 操作
            ActionResult result = controller.Register(model);

            // 断言
            Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));
            RedirectToRouteResult redirectResult = (RedirectToRouteResult)result;
            Assert.AreEqual("Home", redirectResult.RouteValues["controller"]);
            Assert.AreEqual("Index", redirectResult.RouteValues["action"]);
        }
        public void Register_Post_ReturnsViewIfModelStateIsInvalid()
        {
            // 排列
            AccountController controller = GetAccountController();
            RegisterModel model = new RegisterModel()
            {
                UserName = "******",
                Email = "goodEmail",
                Password = "******",
                ConfirmPassword = "******"
            };
            controller.ModelState.AddModelError("", "虚拟错误消息。");

            // 操作
            ActionResult result = controller.Register(model);

            // 断言
            Assert.IsInstanceOfType(result, typeof(ViewResult));
            ViewResult viewResult = (ViewResult)result;
            Assert.AreEqual(model, viewResult.ViewData.Model);
            Assert.AreEqual(10, viewResult.ViewData["PasswordLength"]);
        }
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // 尝试注册用户
                MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email);

                if (createStatus == MembershipCreateStatus.Success)
                {
                    FormsService.SignIn(model.UserName, false /* createPersistentCookie */);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
                }
            }

            // 如果我们进行到这一步时某个地方出错,则重新显示表单
            ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
            return View(model);
        }