public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { // Attempt to register the user try { WebSecurity.CreateUserAndAccount(model.UserName, model.Password); WebSecurity.Login(model.UserName, model.Password); return RedirectToAction("Index", "Home"); } catch (MembershipCreateUserException e) { ModelState.AddModelError("", ErrorCodeToString(e.StatusCode)); } } // If we got this far, something failed, redisplay form return View(model); }
public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { // Attempt to register the user MembershipCreateStatus createStatus; Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus); if (createStatus == MembershipCreateStatus.Success) { FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */); return RedirectToAction("Index", "Home"); } else { ModelState.AddModelError("", ErrorCodeToString(createStatus)); } } // If we got this far, something failed, redisplay form return View(model); }
public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { // Tentare di registrare l'utente 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)); } } // Se si arriva a questo punto, significa che si è verificato un errore, rivisualizzare il form ViewData["PasswordLength"] = MembershipService.MinPasswordLength; return View(model); }
public void Register_Post_ReturnsViewIfModelStateIsInvalid() { // Disposizione AccountController controller = GetAccountController(); RegisterModel model = new RegisterModel() { UserName = "******", Email = "goodEmail", Password = "******", ConfirmPassword = "******" }; controller.ModelState.AddModelError("", "Messaggio di errore fittizio."); // Azione ActionResult result = controller.Register(model); // Asserzione Assert.IsInstanceOfType(result, typeof(ViewResult)); ViewResult viewResult = (ViewResult)result; Assert.AreEqual(model, viewResult.ViewData.Model); Assert.AreEqual(10, viewResult.ViewData["PasswordLength"]); }
public void Register_Post_ReturnsViewIfRegistrationFails() { // Disposizione AccountController controller = GetAccountController(); RegisterModel model = new RegisterModel() { UserName = "******", Email = "goodEmail", Password = "******", ConfirmPassword = "******" }; // Azione ActionResult result = controller.Register(model); // Asserzione Assert.IsInstanceOfType(result, typeof(ViewResult)); ViewResult viewResult = (ViewResult)result; Assert.AreEqual(model, viewResult.ViewData.Model); Assert.AreEqual("Nome utente già esistente. Immetterne un altro.", controller.ModelState[""].Errors[0].ErrorMessage); Assert.AreEqual(10, viewResult.ViewData["PasswordLength"]); }
public void Register_Post_ReturnsRedirectOnSuccess() { // Disposizione AccountController controller = GetAccountController(); RegisterModel model = new RegisterModel() { UserName = "******", Email = "goodEmail", Password = "******", ConfirmPassword = "******" }; // Azione ActionResult result = controller.Register(model); // Asserzione Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult)); RedirectToRouteResult redirectResult = (RedirectToRouteResult)result; Assert.AreEqual("Home", redirectResult.RouteValues["controller"]); Assert.AreEqual("Index", redirectResult.RouteValues["action"]); }