Esempio n. 1
0
        public ActionResult Create(UserModel model)
        {
            try
            {
                // Attempt to register the user
                MembershipCreateStatus createStatus;
                Membership.CreateUser(model.Username, model.Password, model.Email, passwordQuestion: null, passwordAnswer: null, isApproved: true, providerUserKey: null, status: out createStatus);

                if (createStatus == MembershipCreateStatus.Success)
                {
                    Roles.AddUserToRole(model.Username, model.Role);
                }

                return RedirectToAction("Index", "Users");
            }
            catch
            {
                return View(model);
            }
        }
Esempio n. 2
0
        //
        // GET: /Users/Edit/5
        public ActionResult Edit(string username)
        {
            var user = Membership.GetUser(username);
            if (user == null)
            {
                RedirectToAction("Index", "Users");
            }

            var model = new UserModel();
            model.Username = user.UserName;
            model.Email = user.Email;
            model.Role = Roles.GetRolesForUser(user.UserName)[0];
            model.Roles = Roles.GetAllRoles();

            return View(model);
        }
Esempio n. 3
0
 //
 // GET: /Users/Create
 public ActionResult Create()
 {
     var model = new UserModel();
     model.Roles = Roles.GetAllRoles();
     return View(model);
 }
Esempio n. 4
0
        public ActionResult Edit(UserModel model)
        {
            try
            {
                var user = Membership.GetUser(model.Username);
                if (user == null)
                {
                    RedirectToAction("Index", "Users");
                }

                user.Email = model.Email;

                Membership.UpdateUser(user);

                var role = Roles.GetRolesForUser(user.UserName)[0];

                if (role != model.Role)
                {
                    Roles.RemoveUserFromRole(user.UserName, role);
                    Roles.AddUserToRole(user.UserName, model.Role);
                }

                return RedirectToAction("Index");
            }
            catch
            {
                model.Roles = Roles.GetAllRoles();
                return View(model);
            }
        }