Ejemplo n.º 1
0
        public ActionResult CreateAdministrator(CreateAccountViewModel viewModel)
        {
            if (Membership.GetAllUsers().Count != 0) // This page is only accessible with no registered users in the db
                return new HttpUnauthorizedResult();
            if (!ModelState.IsValid)
                return View(viewModel);

            MembershipCreateStatus status;

            var user = Membership.CreateUser(viewModel.Username, viewModel.Password,
                viewModel.Email, null, null, true, null, out status);
            if (status != MembershipCreateStatus.Success)
                return View(viewModel);
            user.LastActivityDate = DateTime.Now;
            if (!Roles.GetAllRoles().Contains("Administrator"))
                Roles.CreateRole("Administrator");
            Roles.AddUserToRole(viewModel.Username, "Administrator");

            FormsAuthentication.SetAuthCookie(viewModel.Username, viewModel.RememberMe);
            return RedirectToAction("Index", "Web");
        }
Ejemplo n.º 2
0
        public ActionResult CreateAccount(CreateAccountViewModel viewModel)
        {
            if (!ModelState.IsValid)
                return View(viewModel);
            string error;
            if (!Recapcha.ValidateCapcha(Request, Request.Form["recaptcha_challenge_field"],
                                    Request.Form["recaptcha_response_field"], out error))
            {
                viewModel.CapchaError = error;
                return View(viewModel);
            }

            MembershipCreateStatus status;

            var user = Membership.CreateUser(viewModel.Username, viewModel.Password,
                viewModel.Email, null, null, true, null, out status);
            if (status != MembershipCreateStatus.Success)
                return View(viewModel);
            user.LastActivityDate = DateTime.Now;

            FormsAuthentication.SetAuthCookie(viewModel.Username, viewModel.RememberMe);
            return RedirectToAction("Index", "Web");
        }