public ActionResult Register(RegisterModel model)
        {
            GetBrokenRulesFor(model);
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    WebSecurity.CreateUserAndAccount(model.UserName, model.Password, propertyValues: new { Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, City = model.City, State = model.State, Zip = model.Zip });
                    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);
        }
 private void GetBrokenRulesFor(RegisterModel model)
 {
     if (!IsValid(model, ValidType.UserName))
     {
         ModelState.AddModelError("", ErrorCodeToString(MembershipCreateStatus.DuplicateUserName));
     }
     if (!IsValid(model, ValidType.Email))
     {
         ModelState.AddModelError("", ErrorCodeToString(MembershipCreateStatus.DuplicateEmail));
     }
 }
        public bool IsValid(RegisterModel model, ValidType validType)
        {
            foreach (UserProfile UP in db.UserProfiles.ToList())
            {
                if (validType == ValidType.UserName)
                {
                    if (UP.UserName == model.UserName)
                    {
                        return false;
                    }
                }
                if (validType == ValidType.Email)
                {
                    if (UP.Email == model.Email)
                    {
                        return false;
                    }
                }
            }

            return true;
        }