示例#1
0
        public ActionResult Register(TestWeb2.Models.RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
                    WebSecurity.Login(model.UserName, model.Password);

                    if (model.IsAdmin)
                    {
                        Admin newAdmin = new Admin(model.UserName);
                        db.Admins.Add(newAdmin);
                        db.SaveChanges();
                        return(RedirectToAction("Index", "Admin"));
                    }
                    else
                    {
                        Volunteer newVolunteer = new Volunteer(model.UserName);
                        db.Volunteers.Add(newVolunteer);
                        db.SaveChanges();
                        return(RedirectToAction("Index", "Home"));
                    }
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
示例#2
0
        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);
        }